From e0f3d0a042c0cc33d677c1dce8c9761d4c32f0ad Mon Sep 17 00:00:00 2001 From: manny Date: Fri, 23 Sep 2022 03:59:40 -0400 Subject: [PATCH 1/2] add simple interest and compound interest --- DIRECTORY.md | 1 + README.md | 1 + src/math/interest.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/math/mod.rs | 2 ++ 4 files changed, 59 insertions(+) create mode 100644 src/math/interest.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index bd3c98b36f8..ce6f9bbf8b9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/README.md b/README.md index 23aca25f767..d24ed4faa89 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/math/interest.rs b/src/math/interest.rs new file mode 100644 index 00000000000..8d96773d22d --- /dev/null +++ b/src/math/interest.rs @@ -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 { + // 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()); + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 96752f20245..515707d69e6 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -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; @@ -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; From 5f27f6d1d62cf3b0d2277ead50c36fc56286334c Mon Sep 17 00:00:00 2001 From: manny Date: Sat, 24 Sep 2022 12:43:01 -0400 Subject: [PATCH 2/2] Fixed link in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d24ed4faa89..83b9e5ff8c5 100644 --- a/README.md +++ b/README.md @@ -74,7 +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) +- [x] [Financial Interest](./src/math/interest.rs) ## [Dynamic Programming](./src/dynamic_programming)