Add integer partition algorithm using dynamic programming #976
+125
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Adds an implementation of the integer partition algorithm using dynamic programming.
The partition function calculates the number of ways to express a positive integer as a sum of positive integers (order doesn't matter). For example, the number 5 can be partitioned in 7 ways:
Algorithm Overview
The implementation uses dynamic programming with a 2D memoization table where:
memo[n][k]represents the number of partitions ofninto at leastkpartsmemo[i][0] = 1(one way to partition into 0 parts)memo[n][k] = memo[n][k-1] + memo[n-k-1][k]This approach is based on the mathematical principle that:
Changes Made
src/dynamic_programming/integer_partition.rssrc/dynamic_programming/mod.rssrc/dynamic_programming/mod.rsTesting
All tests pass:
cargo test integer_partitionTest cases include:
partition(5)→ 7partition(7)→ 15partition(100)→ 190,569,292partition(1000)→ 24,061,467,864,032,622,473,692,149,727,991References
Checklist