Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: Add Aliquot Sum implementation (TheAlgorithms#412)
  • Loading branch information
SpiderMath authored Oct 30, 2022
commit bfcdde5bb11432dfa142176dc49a6dc80ec794ea
40 changes: 40 additions & 0 deletions src/math/aliquot_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// Aliquot sum of a number is defined as the sum of the proper divisors of
/// a number, i.e. all the divisors of a number apart from the number itself
/// For example: The aliquot sum of 6 is (1 + 2 + 3) = 6, and that of 15 is
/// (1 + 3 + 5) = 9
/// Wikipedia article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum

pub fn aliquot_sum(number: u64) -> u64 {
if number == 1 || number == 0 {
return 0;
}
let mut sum: u64 = 0;

for i in 1..(number / 2 + 1) {
if number % i == 0 {
sum += i;
}
}

sum
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn one_digit_number() {
assert_eq!(aliquot_sum(6), 6);
}

#[test]
fn two_digit_number() {
assert_eq!(aliquot_sum(15), 9);
}

#[test]
fn three_digit_number() {
assert_eq!(aliquot_sum(343), 57);
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod aliquot_sum;
mod amicable_numbers;
mod armstrong_number;
mod baby_step_giant_step;
Expand Down Expand Up @@ -35,6 +36,7 @@ mod square_root;
mod trial_division;
mod zellers_congruence_algorithm;

pub use self::aliquot_sum::aliquot_sum;
pub use self::amicable_numbers::amicable_pairs_under_n;
pub use self::armstrong_number::is_armstrong_number;
pub use self::baby_step_giant_step::baby_step_giant_step;
Expand Down