Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

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:

  • 5
  • 4 + 1
  • 3 + 2
  • 3 + 1 + 1
  • 2 + 2 + 1
  • 2 + 1 + 1 + 1
  • 1 + 1 + 1 + 1 + 1

Algorithm Overview

The implementation uses dynamic programming with a 2D memoization table where:

  • memo[n][k] represents the number of partitions of n into at least k parts
  • Base case: memo[i][0] = 1 (one way to partition into 0 parts)
  • Recurrence relation: memo[n][k] = memo[n][k-1] + memo[n-k-1][k]

This approach is based on the mathematical principle that:

The number of partitions of n into at least k parts equals the number of partitions into exactly k parts plus the number of partitions into at least k-1 parts.

Changes Made

  • Added src/dynamic_programming/integer_partition.rs
  • Added module declaration in src/dynamic_programming/mod.rs
  • Added public export in src/dynamic_programming/mod.rs

Testing

All tests pass:

cargo test integer_partition

Test cases include:

  • partition(5) → 7
  • partition(7) → 15
  • partition(100) → 190,569,292
  • partition(1000) → 24,061,467,864,032,622,473,692,149,727,991
  • ✅ Invalid inputs (0, negative) → panic with assertion error

References

Checklist

  • Code follows repository style guidelines
  • All tests pass
  • Documentation includes examples
  • Function is publicly exported
  • No compiler warnings

@codecov-commenter
Copy link

codecov-commenter commented Dec 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.77%. Comparing base (b77770e) to head (a509064).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #976      +/-   ##
==========================================
+ Coverage   95.75%   95.77%   +0.01%     
==========================================
  Files         350      351       +1     
  Lines       22892    22934      +42     
==========================================
+ Hits        21920    21964      +44     
+ Misses        972      970       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged. Clippy error is a CI issue. No problem on other machines.

@siriak siriak merged commit 43299ac into TheAlgorithms:master Dec 25, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants