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
30namespace math {
31
36uint64_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
57static 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
74int main() {
75 test(); // run the self-test implementations
76 return 0;
77}
static void test()
Self-test implementations.
int main()
Main function.
for assert
uint64_t aliquot_sum(const uint64_t num)
to return the aliquot sum of a number