Skip to content

Commit f7a2e3d

Browse files
committed
Quality of life
FIX: initlized sum to 1 instead of adding it before return CHORE: cleaned documentation aswell as adding new documentation, namespace math added
1 parent 6027480 commit f7a2e3d

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

math/check_amicable_pair.cpp

+37-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
/**
2-
*
32
* @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
54
* pair](https://en.wikipedia.org/wiki/Amicable_numbers) or not.
65
*
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)
1116
*/
12-
#include <cassert>
13-
#include <iostream>
17+
#include <cassert> // for assert
18+
#include <iostream> // for cout
1419

1520
/**
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
1727
* of an integer.
18-
* @param num First number.
28+
* @param num selected number.
1929
* @return Sum of the proper divisor of the number.
2030
*/
2131
int sum_of_divisor(int num) {
2232
// Variable to store the sum of all proper divisors.
23-
int sum = 0;
33+
int sum = 1;
2434
// Below loop condition helps to reduce Time complexity by a factor of
2535
// square root of the number.
2636
for (int div = 2; div * div <= num; ++div) {
@@ -35,11 +45,11 @@ int sum_of_divisor(int num) {
3545
}
3646
}
3747
}
38-
return sum + 1;
48+
return sum;
3949
}
4050

4151
/**
42-
* Function to check whether the pair is amicable or not.
52+
* @brief Function to check whether the pair is amicable or not.
4353
* @param x First number.
4454
* @param y Second number.
4555
* @return `true` if the pair is amicable
@@ -48,25 +58,27 @@ int sum_of_divisor(int num) {
4858
bool are_amicable(int x, int y) {
4959
return (sum_of_divisor(x) == y) && (sum_of_divisor(y) == x);
5060
}
61+
} // namespace math
5162

5263
/**
53-
* Function for testing the is_amicable() with
54-
* all the test cases.
64+
* @brief Self-test implementations
65+
* @returns void
5566
*/
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;
6375
}
6476

6577
/**
66-
* Main Function
78+
* @brief Main function
79+
* @returns 0 on exit
6780
*/
6881
int main() {
69-
test();
70-
std::cout << "Assertion Success." << std::endl;
82+
tests(); // perform self-tests implementations
7183
return 0;
7284
}

0 commit comments

Comments
 (0)