Skip to content

Commit 531d90a

Browse files
authored
Add Sum of Geometric Progression (TheAlgorithms#597)
1 parent 7ec4785 commit 531d90a

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
* [Gaussian Elimination](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_elimination.rs)
173173
* [Gaussian Error Linear Unit](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gaussian_error_linear_unit.rs)
174174
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/gcd_of_n_numbers.rs)
175+
* [Geometric Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/geometric_series.rs)
175176
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Rust/blob/master/src/math/greatest_common_divisor.rs)
176177
* [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/huber_loss.rs)
177178
* [Interest](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interest.rs)
@@ -187,6 +188,7 @@
187188
* [Nthprime](https://github.com/TheAlgorithms/Rust/blob/master/src/math/nthprime.rs)
188189
* [Pascal Triangle](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pascal_triangle.rs)
189190
* [Perfect Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_numbers.rs)
191+
* [Perfect Square](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_square.rs)
190192
* [Pollard Rho](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pollard_rho.rs)
191193
* [Prime Check](https://github.com/TheAlgorithms/Rust/blob/master/src/math/prime_check.rs)
192194
* [Prime Factors](https://github.com/TheAlgorithms/Rust/blob/master/src/math/prime_factors.rs)
@@ -204,6 +206,7 @@
204206
* [Square Pyramidal Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_pyramidal_numbers.rs)
205207
* [Square Root](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_root.rs)
206208
* [Sum Of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs)
209+
* [Sum Of Geometric Progression](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_geometric_progression.rs)
207210
* [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs)
208211
* [Trial Division](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trial_division.rs)
209212
* [Vector Cross Product](https://github.com/TheAlgorithms/Rust/blob/master/src/math/vector_cross_product.rs)

src/math/geometric_series.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Wikipedia : https://en.wikipedia.org/wiki/Geometric_series
33
// Calculate a geometric series.
44

5-
fn geometric_series(nth_term: f64, start_term_a: f64, common_ratio_r: f64) -> Vec<f64> {
5+
pub fn geometric_series(nth_term: f64, start_term_a: f64, common_ratio_r: f64) -> Vec<f64> {
66
let mut series = Vec::new();
77
let mut multiple = 1.0;
88

@@ -14,19 +14,13 @@ fn geometric_series(nth_term: f64, start_term_a: f64, common_ratio_r: f64) -> Ve
1414
series
1515
}
1616

17-
1817
#[cfg(test)]
1918
mod tests {
2019
use super::*;
2120

2221
fn assert_approx_eq(a: f64, b: f64) {
2322
let epsilon = 1e-10;
24-
assert!(
25-
(a - b).abs() < epsilon,
26-
"Expected {}, found {}",
27-
a,
28-
b
29-
);
23+
assert!((a - b).abs() < epsilon, "Expected {}, found {}", a, b);
3024
}
3125

3226
#[test]
@@ -51,6 +45,5 @@ mod tests {
5145
assert_approx_eq(result[1], -4.0);
5246
assert_approx_eq(result[2], -8.0);
5347
assert_approx_eq(result[3], -16.0);
54-
5548
}
5649
}

src/math/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod frizzy_number;
2727
mod gaussian_elimination;
2828
mod gaussian_error_linear_unit;
2929
mod gcd_of_n_numbers;
30+
mod geometric_series;
3031
mod greatest_common_divisor;
3132
mod huber_loss;
3233
mod interest;
@@ -42,6 +43,7 @@ mod newton_raphson;
4243
mod nthprime;
4344
mod pascal_triangle;
4445
mod perfect_numbers;
46+
mod perfect_square;
4547
mod pollard_rho;
4648
mod prime_check;
4749
mod prime_factors;
@@ -59,6 +61,7 @@ mod sprague_grundy_theorem;
5961
mod square_pyramidal_numbers;
6062
mod square_root;
6163
mod sum_of_digits;
64+
mod sum_of_geometric_progression;
6265
mod tanh;
6366
mod trial_division;
6467
mod vector_cross_product;
@@ -96,6 +99,7 @@ pub use self::frizzy_number::get_nth_frizzy;
9699
pub use self::gaussian_elimination::gaussian_elimination;
97100
pub use self::gaussian_error_linear_unit::gaussian_error_linear_unit;
98101
pub use self::gcd_of_n_numbers::gcd;
102+
pub use self::geometric_series::geometric_series;
99103
pub use self::greatest_common_divisor::{
100104
greatest_common_divisor_iterative, greatest_common_divisor_recursive,
101105
greatest_common_divisor_stein,
@@ -114,6 +118,8 @@ pub use self::newton_raphson::find_root;
114118
pub use self::nthprime::nthprime;
115119
pub use self::pascal_triangle::pascal_triangle;
116120
pub use self::perfect_numbers::perfect_numbers;
121+
pub use self::perfect_square::perfect_square;
122+
pub use self::perfect_square::perfect_square_binary_search;
117123
pub use self::pollard_rho::{pollard_rho_factorize, pollard_rho_get_one_factor};
118124
pub use self::prime_check::prime_check;
119125
pub use self::prime_factors::prime_factors;
@@ -131,6 +137,7 @@ pub use self::sprague_grundy_theorem::calculate_grundy_number;
131137
pub use self::square_pyramidal_numbers::square_pyramidal_number;
132138
pub use self::square_root::{fast_inv_sqrt, square_root};
133139
pub use self::sum_of_digits::{sum_digits_iterative, sum_digits_recursive};
140+
pub use self::sum_of_geometric_progression::sum_of_geometric_progression;
134141
pub use self::tanh::tanh;
135142
pub use self::trial_division::trial_division;
136143
pub use self::vector_cross_product::cross_product;

src/math/perfect_square.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
// Author : cyrixninja
22
// Perfect Square : Checks if a number is perfect square number or not
33
// https://en.wikipedia.org/wiki/Perfect_square
4-
fn perfect_square(num: i32) -> bool {
4+
pub fn perfect_square(num: i32) -> bool {
55
if num < 0 {
66
return false;
77
}
88
let sqrt_num = (num as f64).sqrt() as i32;
99
sqrt_num * sqrt_num == num
1010
}
1111

12-
fn perfect_square_binary_search(n: i32) -> bool {
12+
pub fn perfect_square_binary_search(n: i32) -> bool {
1313
if n < 0 {
1414
return false;
1515
}
1616

1717
let mut left = 0;
1818
let mut right = n;
19-
19+
2020
while left <= right {
2121
let mid = (left + right) / 2;
2222
let mid_squared = mid * mid;
2323

24-
if mid_squared == n {
25-
return true;
26-
} else if mid_squared > n {
27-
right = mid - 1;
28-
} else {
29-
left = mid + 1;
24+
match mid_squared.cmp(&n) {
25+
std::cmp::Ordering::Equal => return true,
26+
std::cmp::Ordering::Greater => right = mid - 1,
27+
std::cmp::Ordering::Less => left = mid + 1,
3028
}
3129
}
3230

@@ -39,21 +37,21 @@ mod tests {
3937

4038
#[test]
4139
fn test_perfect_square() {
42-
assert!(perfect_square(9) == true);
43-
assert!(perfect_square(81) == true);
44-
assert!(perfect_square(4) == true);
45-
assert!(perfect_square(0) == true);
46-
assert!(perfect_square(3) == false);
47-
assert!(perfect_square(-19) == false);
40+
assert!(perfect_square(9));
41+
assert!(perfect_square(81));
42+
assert!(perfect_square(4));
43+
assert!(perfect_square(0));
44+
assert!(!perfect_square(3));
45+
assert!(!perfect_square(-19));
4846
}
4947

5048
#[test]
5149
fn test_perfect_square_binary_search() {
52-
assert!(perfect_square_binary_search(9) == true);
53-
assert!(perfect_square_binary_search(81) == true);
54-
assert!(perfect_square_binary_search(4) == true);
55-
assert!(perfect_square_binary_search(0) == true);
56-
assert!(perfect_square_binary_search(3) == false);
57-
assert!(perfect_square_binary_search(-19) == false);
50+
assert!(perfect_square_binary_search(9));
51+
assert!(perfect_square_binary_search(81));
52+
assert!(perfect_square_binary_search(4));
53+
assert!(perfect_square_binary_search(0));
54+
assert!(!perfect_square_binary_search(3));
55+
assert!(!perfect_square_binary_search(-19));
5856
}
5957
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Author : cyrixninja
2+
// Find the Sum of Geometric Progression
3+
// Wikipedia: https://en.wikipedia.org/wiki/Geometric_progression
4+
5+
pub fn sum_of_geometric_progression(first_term: f64, common_ratio: f64, num_of_terms: i32) -> f64 {
6+
if common_ratio == 1.0 {
7+
// Formula for sum if the common ratio is 1
8+
return (num_of_terms as f64) * first_term;
9+
}
10+
11+
// Formula for finding the sum of n terms of a Geometric Progression
12+
(first_term / (1.0 - common_ratio)) * (1.0 - common_ratio.powi(num_of_terms))
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_sum_of_geometric_progression() {
21+
let result_1 = sum_of_geometric_progression(1.0, 2.0, 10);
22+
assert_eq!(result_1, 1023.0);
23+
24+
let result_2 = sum_of_geometric_progression(1.0, 10.0, 5);
25+
assert_eq!(result_2, 11111.0);
26+
27+
let result_3 = sum_of_geometric_progression(9.0, 2.5, 5);
28+
assert_eq!(result_3, 579.9375);
29+
}
30+
}

0 commit comments

Comments
 (0)