TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
Include dependency graph for count_of_trailing_ciphers_in_factorial_n.cpp:

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for assert
 
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

Definition in file count_of_trailing_ciphers_in_factorial_n.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 96 of file count_of_trailing_ciphers_in_factorial_n.cpp.

96 {
97 test(); // run self-test implementations
98 return 0;
99}
static void test()
Self-test implementations.

◆ 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!.

Definition at line 41 of file count_of_trailing_ciphers_in_factorial_n.cpp.

41 {
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}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 60 of file count_of_trailing_ciphers_in_factorial_n.cpp.

60 {
61 // 1st test
62 std::cout << "1st test ";
63 assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
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::
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::
89 std::cout << "passed" << std::endl;
90}
uint64_t numberOfCiphersInFactorialN(uint64_t n)
Function to count the number of the trailing ciphers.