forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_amicable_pair.cpp
72 lines (68 loc) · 1.92 KB
/
check_amicable_pair.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
*
* @file
* \brief A C++ Program to check whether a pair of number is [amicable
* pair](https://en.wikipedia.org/wiki/Amicable_numbers) or not.
*
* \details
* Amicable Pair are two positive integers such that sum of the proper divisor
* of each number is equal to the other number.
* @author iamnambiar
*/
#include <cassert>
#include <iostream>
/**
* Function to calculate the sum of all the proper divisor
* of an integer.
* @param num First number.
* @return Sum of the proper divisor of the number.
*/
int sum_of_divisor(int num) {
// 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;
}
/**
* Function to check whether the pair is amicable or not.
* @param x First number.
* @param y Second number.
* @return `true` if the pair is amicable
* @return `false` if the pair is not amicable
*/
bool are_amicable(int x, int y) {
return (sum_of_divisor(x) == y) && (sum_of_divisor(y) == x);
}
/**
* Function for testing the is_amicable() with
* all the test cases.
*/
void test() {
// 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);
}
/**
* Main Function
*/
int main() {
test();
std::cout << "Assertion Success." << std::endl;
return 0;
}