Threads
count_of_traili ( 18560 ) - count_of_traili ( 18560 ) stack: main(bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp:95)
/**
* @file
* @brief [Count the number of
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus) in `n!` implementation
* @details
* Given an integer number as input. The goal is to find the number of trailing
zeroes in the factorial calculated for
* that number. A factorial of a number N is a product of all numbers in the
range [1, N].
* We know that we get a trailing zero only if the number is multiple of 10 or
has a factor pair (2,5). In all factorials of
* any number greater than 5, we have many 2s more than 5s in the prime
factorization of that number. Dividing a
* number by powers of 5 will give us the count of 5s in its factors. So, the
number of 5s will tell us the number of trailing zeroes.
* @author [Swastika Gupta](https://github.com/Swastyy)
*/
#include /// for assert
#include /// for IO operations
/**
* @namespace bit_manipulation
* @brief Bit manipulation algorithms
*/
namespace bit_manipulation {
/**
* @namespace count_of_trailing_ciphers_in_factorial_n
* @brief Functions for the [Count the number of
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus)
* in `n!` implementation
*/
namespace count_of_trailing_ciphers_in_factorial_n {
/**
* @brief Function to count the number of the trailing ciphers
* @param n number for which `n!` ciphers are returned
* @return count, Number of ciphers in `n!`.
*/
uint64_t numberOfCiphersInFactorialN(uint64_t n) {
// count is to store the number of 5's in factorial(n)
uint64_t count = 0;
// Keep dividing n by powers of
// 5 and update count
for (uint64_t i = 5; n / i >= 1; i *= 5) {
count += static_cast(n) / i;
}
return count;
}
} // namespace count_of_trailing_ciphers_in_factorial_n
} // namespace bit_manipulation
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// 1st test
std::cout << "1st test ";
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
numberOfCiphersInFactorialN(395) == 97);
std::cout << "passed" << std::endl;
// 2nd test
std::cout << "2nd test ";
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
numberOfCiphersInFactorialN(977) == 242);
std::cout << "passed" << std::endl;
// 3rd test
std::cout << "3rd test ";
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
numberOfCiphersInFactorialN(871) == 215);
std::cout << "passed" << std::endl;
// 4th test
std::cout << "4th test ";
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
numberOfCiphersInFactorialN(239) == 57);
std::cout << "passed" << std::endl;
// 5th test
std::cout << "5th test ";
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
numberOfCiphersInFactorialN(0) == 0);
std::cout << "passed" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}
Variables All
No. | From | Name | Value |
---|---|---|---|
NA | NA | NA | NA |
Process | Thread Filter |
---|---|
18560 count_of_traili | 18560 count_of_traili |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | count_of_traili | 18560 | 18560 | count_of_traili | 1st test passed |
2 | count_of_traili | 18560 | 18560 | count_of_traili | 2nd test passed |
3 | count_of_traili | 18560 | 18560 | count_of_traili | 3rd test passed |
4 | count_of_traili | 18560 | 18560 | count_of_traili | 4th test passed |
5 | count_of_traili | 18560 | 18560 | count_of_traili | 5th test passed |
END | 0 | 0 | 0 | 0 | 0 |
×
Commands and Shortcuts
No. | Command | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | algorithms-cpp |
Update: | 20240212 |
Commit: | 2dadbf73f |
Source Code: | bit_manipulation.count_of_trailing_ciphers_in_factorial_n |
BuildTool: | cpp17 |
Compiler: | cpp17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |