forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliquot_sum.rs
40 lines (33 loc) · 881 Bytes
/
aliquot_sum.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
}
}