TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
aliquot_sum.cpp File Reference

Program to return the Aliquot Sum of a number. More...

#include <cassert>
#include <cstdint>
#include <iostream>
Include dependency graph for aliquot_sum.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

uint64_t math::aliquot_sum (const uint64_t num)
 to return the aliquot sum of a number
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Program to return the Aliquot Sum of a number.

The Aliquot sum \(s(n)\) of a non-negative integer n is the sum of all proper divisors of n, that is, all the divisors of n, other than itself.

Formula:

\[ s(n) = \sum_{d|n, d\neq n}d. \]

For example; \(s(18) = 1 + 2 + 3 + 6 + 9 = 21 \)

Author
SpiderMath

Definition in file aliquot_sum.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 74 of file aliquot_sum.cpp.

74 {
75 test(); // run the self-test implementations
76 return 0;
77}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 57 of file aliquot_sum.cpp.

57 {
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}
uint64_t aliquot_sum(const uint64_t num)
to return the aliquot sum of a number