Skip to content

Commit 01791bc

Browse files
Merge branch 'master' into bug_in_ncr_modulo_p_patch
2 parents 1f65077 + e3f0551 commit 01791bc

File tree

4 files changed

+138
-28
lines changed

4 files changed

+138
-28
lines changed

.github/workflows/directory_writer.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
name: Directory writer
22
on:
3-
push:
4-
branches:
5-
- main
63
schedule:
74
# ┌───────────── minute (0 - 59)
85
# │ ┌───────────── hour (0 - 23)

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
2121
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
2222
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
23+
* [next higher number with same number of set bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp)
2324
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
2425
* [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp)
2526
* [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @file
3+
* @brief [Next higher number with same number of set bits]
4+
* (https://www.geeksforgeeks.org/next-higher-number-with-same-number-of-set-bits/)
5+
* implementation
6+
*
7+
* @details
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).
12+
*
13+
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
14+
* set bit in computer terms.
15+
* @author [Kunal Nayak](https://github.com/Kunal766)
16+
*/
17+
18+
#include <cassert> /// for assert
19+
#include <iostream> /// for IO operations
20+
21+
/**
22+
* @namespace bit_manipulation
23+
* @brief Bit manipulation algorithms
24+
*/
25+
namespace bit_manipulation {
26+
27+
/**
28+
* @brief The main function implements checking the next number
29+
* @param x the number that will be calculated
30+
* @returns a number
31+
*/
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;
65+
}
66+
67+
} // namespace bit_manipulation
68+
69+
/**
70+
* @brief Self-test implementations
71+
* @returns void
72+
*/
73+
static void test() {
74+
// x = 4 return 8
75+
assert(bit_manipulation::next_higher_number(4) == 8);
76+
// x = 6 return 9
77+
assert(bit_manipulation::next_higher_number(6) == 9);
78+
// x = 13 return 14
79+
assert(bit_manipulation::next_higher_number(13) == 14);
80+
// x = 64 return 128
81+
assert(bit_manipulation::next_higher_number(64) == 128);
82+
// x = 15 return 23
83+
assert(bit_manipulation::next_higher_number(15) == 23);
84+
// x= 32 return 64
85+
assert(bit_manipulation::next_higher_number(32) == 64);
86+
// x = 97 return 98
87+
assert(bit_manipulation::next_higher_number(97) == 98);
88+
// x = 1024 return 2048
89+
assert(bit_manipulation::next_higher_number(1024) == 2048);
90+
91+
std::cout << "All test cases have successfully passed!" << std::endl;
92+
}
93+
/**
94+
* @brief Main function
95+
* @returns 0 on exit
96+
*/
97+
int main() {
98+
test(); // run self-test implementations
99+
return 0;
100+
}

math/check_amicable_pair.cpp

Lines changed: 37 additions & 25 deletions
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)