1
1
/* *
2
- *
3
2
* @file
4
- * \ brief A C++ Program to check whether a pair of number is [amicable
3
+ * @ brief A C++ Program to check whether a pair of numbers is an [amicable
5
4
* pair](https://en.wikipedia.org/wiki/Amicable_numbers) or not.
6
5
*
7
- * \details
8
- * Amicable Pair are two positive integers such that sum of the proper divisor
9
- * of each number is equal to the other number.
10
- * @author iamnambiar
6
+ * @details
7
+ * An Amicable Pair is two positive integers such that the sum of the proper
8
+ * divisor for each number is equal to the other number.
9
+ *
10
+ * @note Remember that a proper divisor is any positive whole number that
11
+ * divides into a selected number, apart from the selected number itself, and
12
+ * returns a positive integer. for example 1, 2 and 5 are all proper divisors
13
+ * of 10.
14
+ *
15
+ * @author [iamnambiar](https://github.com/iamnambiar)
11
16
*/
12
- #include < cassert>
13
- #include < iostream>
17
+ #include < cassert> // for assert
18
+ #include < iostream> // for cout
14
19
15
20
/* *
16
- * Function to calculate the sum of all the proper divisor
21
+ * @brief Mathematical algorithms
22
+ * @namespace
23
+ */
24
+ namespace math {
25
+ /* *
26
+ * @brief Function to calculate the sum of all the proper divisor
17
27
* of an integer.
18
- * @param num First number.
28
+ * @param num selected number.
19
29
* @return Sum of the proper divisor of the number.
20
30
*/
21
31
int sum_of_divisor (int num) {
22
32
// Variable to store the sum of all proper divisors.
23
- int sum = 0 ;
33
+ int sum = 1 ;
24
34
// Below loop condition helps to reduce Time complexity by a factor of
25
35
// square root of the number.
26
36
for (int div = 2 ; div * div <= num; ++div ) {
@@ -35,11 +45,11 @@ int sum_of_divisor(int num) {
35
45
}
36
46
}
37
47
}
38
- return sum + 1 ;
48
+ return sum;
39
49
}
40
50
41
51
/* *
42
- * Function to check whether the pair is amicable or not.
52
+ * @brief Function to check whether the pair is amicable or not.
43
53
* @param x First number.
44
54
* @param y Second number.
45
55
* @return `true` if the pair is amicable
@@ -48,25 +58,27 @@ int sum_of_divisor(int num) {
48
58
bool are_amicable (int x, int y) {
49
59
return (sum_of_divisor (x) == y) && (sum_of_divisor (y) == x);
50
60
}
61
+ } // namespace math
51
62
52
63
/* *
53
- * Function for testing the is_amicable() with
54
- * all the test cases.
64
+ * @brief Self-test implementations
65
+ * @returns void
55
66
*/
56
- void test () {
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 );
67
+ void tests () {
68
+ assert (math::are_amicable (220 , 284 ) == true );
69
+ assert (math::are_amicable (6368 , 6232 ) == true );
70
+ assert (math::are_amicable (458 , 232 ) == false );
71
+ assert (math::are_amicable (17296 , 18416 ) == true );
72
+ assert (math::are_amicable (18416 , 17296 ) == true );
73
+
74
+ std::cout << " all tests have passed" << std::endl;
63
75
}
64
76
65
77
/* *
66
- * Main Function
78
+ * @brief Main function
79
+ * @returns 0 on exit
67
80
*/
68
81
int main () {
69
- test ();
70
- std::cout << " Assertion Success." << std::endl;
82
+ tests (); // perform self-tests implementations
71
83
return 0 ;
72
84
}
0 commit comments