Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
* [Fast Fourier Transform](https://github.com/TheAlgorithms/Rust/blob/master/src/math/fast_fourier_transform.rs)
* [Armstrong Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/armstrong_number.rs)
* [Permuted Congruential Random Number Generator](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs)
* [Financial Interest](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interest.rs)
* Searching
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)
* [Binary Search Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ These are for demonstration purposes only.
- [x] [Permuted Congruential Random Number Generator](./src/math/random.rs)
- [x] [Zeller's Congruence Algorithm](./src/math/zellers_congruence_algorithm.rs)
- [x] [Karatsuba Multiplication Algorithm](./src/math/karatsuba_multiplication.rs)
- [x] [Financial Interest](./src/master/src/math/interest.rs)

## [Dynamic Programming](./src/dynamic_programming)

Expand Down
55 changes: 55 additions & 0 deletions src/math/interest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// value of e
use std::f64::consts::E;

// function to calculate simple interest
pub fn simple_interest(principal: f64, annual_rate: f64, years: f64) -> (f64, f64) {
let interest = principal * annual_rate * years;
let value = principal * (1.0 + (annual_rate * years));

println!("Interest earned: {:?}", interest);
println!("Future value: {:?}", value);

(interest, value)
}

// function to calculate compound interest compounded over periods or continuously
pub fn compound_interest(principal: f64, annual_rate: f64, years: f64, period: Option<f64>) -> f64 {
// checks if the the period is None type, if so calculates continuous compounding interest
let value = if period.is_none() {
principal * E.powf(annual_rate * years)
} else {
// unwraps the option type or defaults to 0 if None type and assigns it to prim_period
let prim_period: f64 = period.unwrap_or(0.0);
// checks if the period is less than or equal to zero
if prim_period <= 0.0_f64 {
return f64::NAN;
}
principal * (1.0 + (annual_rate / prim_period).powf(prim_period * years))
};
println!("Future value: {:?}", value);
value
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let x = 385.65_f64 * 0.03_f64 * 5.0_f64;
let y = 385.65_f64 * (1.0 + (0.03_f64 * 5.0_f64));
assert_eq!(simple_interest(385.65_f64, 0.03_f64, 5.0_f64), (x, y));
}
#[test]
fn test_compounding() {
let x = 385.65_f64 * E.powf(0.03_f64 * 5.0_f64);
assert_eq!(compound_interest(385.65_f64, 0.03_f64, 5.0_f64, None), x);

let y = 385.65_f64 * (1.0 + (0.03_f64 / 5.0_f64).powf(5.0_f64 * 5.0_f64));
assert_eq!(
compound_interest(385.65_f64, 0.03_f64, 5.0_f64, Some(5.0_f64)),
y
);
assert!(compound_interest(385.65_f64, 0.03_f64, 5.0_f64, Some(-5.0_f64)).is_nan());
assert!(compound_interest(385.65_f64, 0.03_f64, 5.0_f64, Some(0.0_f64)).is_nan());
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod faster_perfect_numbers;
mod gaussian_elimination;
mod gcd_of_n_numbers;
mod greatest_common_divisor;
mod interest;
mod karatsuba_multiplication;
mod lcm_of_n_numbers;
mod linear_sieve;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub use self::gcd_of_n_numbers::gcd;
pub use self::greatest_common_divisor::{
greatest_common_divisor_iterative, greatest_common_divisor_recursive,
};
pub use self::interest::{compound_interest, simple_interest};
pub use self::karatsuba_multiplication::multiply;
pub use self::lcm_of_n_numbers::lcm;
pub use self::linear_sieve::LinearSieve;
Expand Down