check_amicable_pair.cpp File Reference

A C++ Program to check whether a pair of number is amicable pair or not. More...

`#include <cassert>`
`#include <iostream>`
Include dependency graph for check_amicable_pair.cpp:

## Functions

int | sum_of_divisor (int num)
bool | are_amicable (int x, int y)
void | test ()
int | main ()

## Detailed Description

A C++ Program to check whether a pair of number is amicable pair or not.

Amicable Pair are two positive integers such that sum of the proper divisor of each number is equal to the other number.

Author
  iamnambiar

## Function Documentation

## are_amicable()

bool are_amicable | ( | int | x,
|  | int | y
| ) |  |

Function to check whether the pair is amicable or not.

Parameters

  x | First number.
  y | Second number.

Returns
  `true` if the pair is amicable
  `false` if the pair is not amicable

{
return (sum_of_divisor(x) == y) && (sum_of_divisor(y) == x);
}
sum_of_divisor
int sum_of_divisor(int num)
Definition: check_amicable_pair.cpp:21
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main Function

{
test();
std::cout << "Assertion Success." << std::endl;
return 0;
}
std::cout
test
void test()
Definition: check_amicable_pair.cpp:56
std::endl
T endl(T... args)
Here is the call graph for this function:

## sum_of_divisor()

int sum_of_divisor | ( | int | num | ) |

Function to calculate the sum of all the proper divisor of an integer.

Parameters

  num | First number.

Returns
  Sum of the proper divisor of the number.

{
// Variable to store the sum of all proper divisors.
int sum = 0;
// Below loop condition helps to reduce Time complexity by a factor of
// square root of the number.
for (int div = 2; div * div <= num; ++div) {
// Check 'div' is divisor of 'num'.
if (num % div == 0) {
// If both divisor are same, add once to 'sum'
if (div == (num / div)) {
sum += div;
} else {
// If both divisor are not the same, add both to 'sum'.
sum += (div + (num / div));
}
}
}
return sum + 1;
}
std::div
T div(T... args)
machine_learning::sum
T sum(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:232
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Function for testing the is_amicable() with all the test cases.

{
// are_amicable(220, 284) returns true.
assert(are_amicable(220, 284) == true);
// are_amicable(6232, 6368) returns true.
assert(are_amicable(6368, 6232) == true);
// are_amicable(458, 232) returns false.
assert(are_amicable(458, 232) == false);
}
are_amicable
bool are_amicable(int x, int y)
Definition: check_amicable_pair.cpp:48
Here is the call graph for this function: