Skip to content

feat: add Simple, Compound, and APR interest calculations#1026

Merged
siriak merged 2 commits intoTheAlgorithms:masterfrom
AliAlimohammadi:add-interest-calculations
Mar 7, 2026
Merged

feat: add Simple, Compound, and APR interest calculations#1026
siriak merged 2 commits intoTheAlgorithms:masterfrom
AliAlimohammadi:add-interest-calculations

Conversation

@AliAlimohammadi
Copy link
Contributor

@AliAlimohammadi AliAlimohammadi commented Mar 6, 2026

Description

Given a principal amount, an interest rate, and a time period, the module computes three flavours of interest return:

  • Simple interest multiplies principal × daily rate × number of days directly:

$$I = p \times r \times t$$

  • Compound interest applies the standard formula, where $r$ is the rate per compounding period and $n$ is the total number of periods:

$$I = p \times \left((1 + r)^n - 1\right)$$

  • APR interest delegates to compound interest after converting the annual percentage rate to a daily rate and years to days, matching real-world lender disclosures:

$$I = p \times \left(\left(1 + \frac{r}{365}\right)^{t \times 365} - 1\right)$$

All three functions validate their inputs and return Result<f64, &'static str>, propagating a descriptive error string rather than panicking on bad input.

  • Time complexity: $O(1)$
  • Space complexity: $O(1)$

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

@AliAlimohammadi AliAlimohammadi requested a review from imp2002 as a code owner March 6, 2026 21:44
@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.04%. Comparing base (a84b940) to head (f10329f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1026      +/-   ##
==========================================
+ Coverage   96.02%   96.04%   +0.01%     
==========================================
  Files         386      386              
  Lines       29011    29102      +91     
==========================================
+ Hits        27858    27951      +93     
+ Misses       1153     1151       -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.

@siriak siriak merged commit 7a690c1 into TheAlgorithms:master Mar 7, 2026
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-interest-calculations branch March 7, 2026 22:32
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