|
| 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 | +} |
0 commit comments