TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
count_of_trailing_ciphers_in_factorial_n.cpp
Go to the documentation of this file.
1
20#include <cassert>
21#include <cstdint>
22#include <iostream>
23
28namespace bit_manipulation {
41uint64_t numberOfCiphersInFactorialN(uint64_t n) {
42 // count is to store the number of 5's in factorial(n)
43 uint64_t count = 0;
44
45 // Keep dividing n by powers of
46 // 5 and update count
47 for (uint64_t i = 5; n / i >= 1; i *= 5) {
48 count += static_cast<uint64_t>(n) / i;
49 }
50
51 return count;
52}
53} // namespace count_of_trailing_ciphers_in_factorial_n
54} // namespace bit_manipulation
55
60static void test() {
61 // 1st test
62 std::cout << "1st test ";
63 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
64 numberOfCiphersInFactorialN(395) == 97);
65 std::cout << "passed" << std::endl;
66
67 // 2nd test
68 std::cout << "2nd test ";
69 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
70 numberOfCiphersInFactorialN(977) == 242);
71 std::cout << "passed" << std::endl;
72
73 // 3rd test
74 std::cout << "3rd test ";
75 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
76 numberOfCiphersInFactorialN(871) == 215);
77 std::cout << "passed" << std::endl;
78
79 // 4th test
80 std::cout << "4th test ";
81 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
82 numberOfCiphersInFactorialN(239) == 57);
83 std::cout << "passed" << std::endl;
84
85 // 5th test
86 std::cout << "5th test ";
87 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
88 numberOfCiphersInFactorialN(0) == 0);
89 std::cout << "passed" << std::endl;
90}
91
96int main() {
97 test(); // run self-test implementations
98 return 0;
99}
uint64_t numberOfCiphersInFactorialN(uint64_t n)
Function to count the number of the trailing ciphers.
static void test()
Self-test implementations.
int main()
Main function.
Functions for the Count the number of ciphers in n! implementation.