Skip to content

Commit e3f0551

Browse files
ewd00010realstealthninjaPanquesito7github-actions[bot]
authored
[fix/docs]: fit check_amicable_pair.cpp to guidelines (#2465)
* 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 * Update math/check_amicable_pair.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_amicable_pair.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_amicable_pair.cpp Co-authored-by: David Leal <[email protected]> * Update math/check_amicable_pair.cpp Co-authored-by: David Leal <[email protected]> * clang-format and clang-tidy fixes for bc87fea * clang-format and clang-tidy fixes for 0a19d1a --------- Co-authored-by: realstealthninja <[email protected]> Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions[bot] <[email protected]>
1 parent 72cd2d0 commit e3f0551

File tree

3 files changed

+101
-92
lines changed

3 files changed

+101
-92
lines changed

bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp

+37-40
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* implementation
66
*
77
* @details
8-
* Given a number x, find next number with same number of 1 bits in it’s binary representation.
9-
* For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine).
10-
* It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012).
8+
* Given a number x, find next number with same number of 1 bits in it’s binary
9+
* representation. For example, consider x = 12, whose binary representation is
10+
* 1100 (excluding leading zeros on 32 bit machine). It contains two logic 1
11+
* bits. The next higher number with two logic 1 bits is 17 (100012).
1112
*
1213
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
1314
* set bit in computer terms.
@@ -28,43 +29,39 @@ namespace bit_manipulation {
2829
* @param x the number that will be calculated
2930
* @returns a number
3031
*/
31-
uint64_t next_higher_number(uint64_t x)
32-
{
33-
34-
uint64_t rightOne;
35-
uint64_t nextHigherOneBit;
36-
uint64_t rightOnesPattern;
37-
38-
uint64_t next = 0;
39-
40-
if(x)
41-
{
42-
43-
// right most set bit
44-
rightOne = x & -(signed)x;
45-
46-
// reset the pattern and set next higher bit
47-
// left part of x will be here
48-
nextHigherOneBit = x + rightOne;
49-
50-
// nextHigherOneBit is now part [D] of the above explanation.
51-
52-
// isolate the pattern
53-
rightOnesPattern = x ^ nextHigherOneBit;
54-
55-
// right adjust pattern
56-
rightOnesPattern = (rightOnesPattern)/rightOne;
57-
58-
// correction factor
59-
rightOnesPattern >>= 2;
60-
61-
// rightOnesPattern is now part [A] of the above explanation.
62-
63-
// integrate new pattern (Add [D] and [A])
64-
next = nextHigherOneBit | rightOnesPattern;
65-
}
66-
67-
return next;
32+
uint64_t next_higher_number(uint64_t x) {
33+
uint64_t rightOne = 0;
34+
uint64_t nextHigherOneBit = 0;
35+
uint64_t rightOnesPattern = 0;
36+
37+
uint64_t next = 0;
38+
39+
if (x) {
40+
// right most set bit
41+
rightOne = x & -static_cast<signed>(x);
42+
43+
// reset the pattern and set next higher bit
44+
// left part of x will be here
45+
nextHigherOneBit = x + rightOne;
46+
47+
// nextHigherOneBit is now part [D] of the above explanation.
48+
49+
// isolate the pattern
50+
rightOnesPattern = x ^ nextHigherOneBit;
51+
52+
// right adjust pattern
53+
rightOnesPattern = (rightOnesPattern) / rightOne;
54+
55+
// correction factor
56+
rightOnesPattern >>= 2;
57+
58+
// rightOnesPattern is now part [A] of the above explanation.
59+
60+
// integrate new pattern (Add [D] and [A])
61+
next = nextHigherOneBit | rightOnesPattern;
62+
}
63+
64+
return next;
6865
}
6966

7067
} // namespace bit_manipulation

math/armstrong_number.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/**
2-
* @file
3-
* @brief Program to check if a number is an [Armstrong/Narcissistic
4-
* number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5-
*
6-
* @details
7-
* Armstrong number or [Narcissistic
8-
* number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9-
* is the sum of its own digits raised to the power of the number of digits.
10-
*
11-
* let n be the narcissistic number,
12-
* \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
13-
* \f$ b > 1 F_b : \N \to \N \f$ where
14-
* \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base \f$b\f$, and
15-
* \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
16-
*
17-
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
18-
*/
19-
#include <cassert> /// for assert
20-
#include <cmath> /// for std::pow
21-
#include <iostream> /// for IO operations
2+
* @file
3+
* @brief Program to check if a number is an [Armstrong/Narcissistic
4+
* number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5+
*
6+
* @details
7+
* Armstrong number or [Narcissistic
8+
* number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9+
* is the sum of its own digits raised to the power of the number of digits.
10+
*
11+
* let n be the narcissistic number,
12+
* \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
13+
* \f$ b > 1 F_b : \N \to \N \f$ where
14+
* \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base
15+
* \f$b\f$, and \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
16+
*
17+
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
18+
*/
19+
#include <cassert> /// for assert
20+
#include <cmath> /// for std::pow
21+
#include <iostream> /// for IO operations
2222

2323
/**
2424
* @brief Function to calculate the total number of digits in the number.
@@ -61,9 +61,9 @@ bool is_armstrong(int number) {
6161
}
6262

6363
/**
64-
* @brief Self-test implementations
65-
* @returns void
66-
*/
64+
* @brief Self-test implementations
65+
* @returns void
66+
*/
6767
static void test() {
6868
// is_armstrong(370) returns true.
6969
assert(is_armstrong(370) == true);
@@ -82,10 +82,10 @@ static void test() {
8282
}
8383

8484
/**
85-
* @brief Main Function
86-
* @returns 0 on exit
87-
*/
85+
* @brief Main Function
86+
* @returns 0 on exit
87+
*/
8888
int main() {
89-
test(); // run self-test implementations
89+
test(); // run self-test implementations
9090
return 0;
9191
}

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 IO operations
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+
static 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 successfully 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)