TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
count_of_trailing_ciphers_in_factorial_n.cpp
Go to the documentation of this file.
1
19
20
#include <cassert>
21
#include <cstdint>
22
#include <iostream>
23
28
namespace
bit_manipulation
{
35
namespace
count_of_trailing_ciphers_in_factorial_n
{
41
uint64_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
60
static
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
96
int
main
() {
97
test
();
// run self-test implementations
98
return
0;
99
}
test
void test()
Definition
caesar_cipher.cpp:100
bit_manipulation::count_of_trailing_ciphers_in_factorial_n::numberOfCiphersInFactorialN
uint64_t numberOfCiphersInFactorialN(uint64_t n)
Function to count the number of the trailing ciphers.
Definition
count_of_trailing_ciphers_in_factorial_n.cpp:41
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
bit_manipulation
for std::string
count_of_trailing_ciphers_in_factorial_n
Functions for the Count the number of ciphers in n! implementation.
bit_manipulation
count_of_trailing_ciphers_in_factorial_n.cpp
Generated by
1.18.0