Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
count_of_trailing_ciphers_in_factorial_n.cpp File Reference

Count the number of ciphers in n! implementation More...

#include <cassert>
#include <iostream>
Include dependency graph for count_of_trailing_ciphers_in_factorial_n.cpp:

Namespaces

namespace  bit_manipulation
 for IO operations
 
namespace  count_of_trailing_ciphers_in_factorial_n
 Functions for the Count the number of ciphers in n! implementation.
 

Functions

uint64_t bit_manipulation::count_of_trailing_ciphers_in_factorial_n::numberOfCiphersInFactorialN (uint64_t n)
 Function to count the number of the trailing ciphers.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Count the number of ciphers in n! implementation

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
95 {
96 test(); // run self-test implementations
97 return 0;
98}
static void test()
Self-test implementations.
Definition count_of_trailing_ciphers_in_factorial_n.cpp:59
Here is the call graph for this function:

◆ numberOfCiphersInFactorialN()

uint64_t bit_manipulation::count_of_trailing_ciphers_in_factorial_n::numberOfCiphersInFactorialN ( uint64_t n)

Function to count the number of the trailing ciphers.

Parameters
nnumber for which n! ciphers are returned
Returns
count, Number of ciphers in n!.
40 {
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}
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
59 {
60 // 1st test
61 std::cout << "1st test ";
62 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
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::
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::
88 std::cout << "passed" << std::endl;
89}
uint64_t numberOfCiphersInFactorialN(uint64_t n)
Function to count the number of the trailing ciphers.
Definition count_of_trailing_ciphers_in_factorial_n.cpp:40
T endl(T... args)
Here is the call graph for this function: