TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
aliquot_sum.cpp
Go to the documentation of this file.
1
22
#include <cassert>
23
#include <cstdint>
24
#include <iostream>
25
30
namespace
math
{
31
36
uint64_t
aliquot_sum
(
const
uint64_t num) {
37
if
(num == 0 || num == 1) {
38
return
0;
// The aliquot sum for 0 and 1 is 0
39
}
40
41
uint64_t sum = 0;
42
43
for
(uint64_t i = 1; i <= num / 2; i++) {
44
if
(num % i == 0) {
45
sum += i;
46
}
47
}
48
49
return
sum;
50
}
51
}
// namespace math
52
57
static
void
test
() {
58
// Aliquot sum of 10 is 1 + 2 + 5 = 8
59
assert(
math::aliquot_sum
(10) == 8);
60
// Aliquot sum of 15 is 1 + 3 + 5 = 9
61
assert(
math::aliquot_sum
(15) == 9);
62
// Aliquot sum of 1 is 0
63
assert(
math::aliquot_sum
(1) == 0);
64
// Aliquot sum of 97 is 1 (the aliquot sum of a prime number is 1)
65
assert(
math::aliquot_sum
(97) == 1);
66
67
std::cout <<
"All the tests have successfully passed!\n"
;
68
}
69
74
int
main
() {
75
test
();
// run the self-test implementations
76
return
0;
77
}
test
static void test()
Self-test implementations.
Definition
aliquot_sum.cpp:57
main
int main()
Main function.
Definition
aliquot_sum.cpp:74
math
for assert
math::aliquot_sum
uint64_t aliquot_sum(const uint64_t num)
to return the aliquot sum of a number
Definition
aliquot_sum.cpp:36
math
aliquot_sum.cpp
Generated by
1.12.0