Skip to content

Commit bd44418

Browse files
SwastyyPanquesito7github-actionsmishraabhinn
authored
feat: Add count_of_trailing_ciphers_in_factorial_n (TheAlgorithms#1543)
* zeroes * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md * clang-format and clang-tidy fixes for 537cd7e * Update count_of_trailing_ciphers_in_factorial_n.cpp * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for a8c85e2 Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
1 parent a6bd936 commit bd44418

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
## Bit Manipulation
1616
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/count_of_set_bits.cpp)
17+
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
1718
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/hamming_distance.cpp)
1819

1920
## Ciphers
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @file
3+
* @brief [Count the number of
4+
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus) in `n!` implementation
5+
* @details
6+
* Given an integer number as input. The goal is to find the number of trailing
7+
zeroes in the factorial calculated for
8+
* that number. A factorial of a number N is a product of all numbers in the
9+
range [1, N].
10+
11+
* We know that we get a trailing zero only if the number is multiple of 10 or
12+
has a factor pair (2,5). In all factorials of
13+
* any number greater than 5, we have many 2s more than 5s in the prime
14+
factorization of that number. Dividing a
15+
* number by powers of 5 will give us the count of 5s in its factors. So, the
16+
number of 5s will tell us the number of trailing zeroes.
17+
* @author [Swastika Gupta](https://github.com/Swastyy)
18+
*/
19+
20+
#include <cassert> /// for assert
21+
#include <iostream> /// for IO operations
22+
23+
/**
24+
* @namespace bit_manipulation
25+
* @brief Bit manipulation algorithms
26+
*/
27+
namespace bit_manipulation {
28+
/**
29+
* @namespace count_of_trailing_ciphers_in_factorial_n
30+
* @brief Functions for the [Count the number of
31+
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus)
32+
* in `n!` implementation
33+
*/
34+
namespace count_of_trailing_ciphers_in_factorial_n {
35+
/**
36+
* @brief Function to count the number of the trailing ciphers
37+
* @param n number for which `n!` ciphers are returned
38+
* @return count, Number of ciphers in `n!`.
39+
*/
40+
uint64_t numberOfCiphersInFactorialN(uint64_t n) {
41+
// count is to store the number of 5's in factorial(n)
42+
uint64_t count = 0;
43+
44+
// Keep dividing n by powers of
45+
// 5 and update count
46+
for (uint64_t i = 5; n / i >= 1; i *= 5) {
47+
count += static_cast<uint64_t>(n) / i;
48+
}
49+
50+
return count;
51+
}
52+
} // namespace count_of_trailing_ciphers_in_factorial_n
53+
} // namespace bit_manipulation
54+
55+
/**
56+
* @brief Self-test implementations
57+
* @returns void
58+
*/
59+
static void test() {
60+
// 1st test
61+
std::cout << "1st test ";
62+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
63+
numberOfCiphersInFactorialN(395) == 97);
64+
std::cout << "passed" << std::endl;
65+
66+
// 2nd test
67+
std::cout << "2nd test ";
68+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
69+
numberOfCiphersInFactorialN(977) == 242);
70+
std::cout << "passed" << std::endl;
71+
72+
// 3rd test
73+
std::cout << "3rd test ";
74+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
75+
numberOfCiphersInFactorialN(871) == 215);
76+
std::cout << "passed" << std::endl;
77+
78+
// 4th test
79+
std::cout << "4th test ";
80+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
81+
numberOfCiphersInFactorialN(239) == 57);
82+
std::cout << "passed" << std::endl;
83+
84+
// 5th test
85+
std::cout << "5th test ";
86+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
87+
numberOfCiphersInFactorialN(0) == 0);
88+
std::cout << "passed" << std::endl;
89+
}
90+
91+
/**
92+
* @brief Main function
93+
* @returns 0 on exit
94+
*/
95+
int main() {
96+
test(); // run self-test implementations
97+
return 0;
98+
}

math/check_prime.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ template <typename T>
2222
bool is_prime(T num) {
2323
bool result = true;
2424
if (num <= 1) {
25-
return 0;
25+
return false;
2626
} else if (num == 2) {
27-
return 1;
27+
return true;
2828
} else if ((num & 1) == 0) {
29-
return 0;
29+
return false;
3030
}
3131
if (num >= 3) {
3232
for (T i = 3; (i * i) <= (num); i = (i + 2)) {
@@ -47,7 +47,7 @@ int main() {
4747
assert(is_prime(50) == false);
4848
assert(is_prime(115249) == true);
4949

50-
int num;
50+
int num = 0;
5151
std::cout << "Enter the number to check if it is prime or not" << std::endl;
5252
std::cin >> num;
5353
bool result = is_prime(num);

0 commit comments

Comments
 (0)