Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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
xFirst number.
ySecond number.
Returns
true if the pair is amicable
false if the pair is not amicable
48 {
49 return (sum_of_divisor(x) == y) && (sum_of_divisor(y) == x);
50}
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

68 {
69 test();
70 std::cout << "Assertion Success." << std::endl;
71 return 0;
72}
void test()
Definition: check_amicable_pair.cpp:56
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
numFirst number.
Returns
Sum of the proper divisor of the number.
21 {
22 // Variable to store the sum of all proper divisors.
23 int sum = 0;
24 // Below loop condition helps to reduce Time complexity by a factor of
25 // square root of the number.
26 for (int div = 2; div * div <= num; ++div) {
27 // Check 'div' is divisor of 'num'.
28 if (num % div == 0) {
29 // If both divisor are same, add once to 'sum'
30 if (div == (num / div)) {
31 sum += div;
32 } else {
33 // If both divisor are not the same, add both to 'sum'.
34 sum += (div + (num / div));
35 }
36 }
37 }
38 return sum + 1;
39}
T div(T... args)
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.

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