Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
Last Updated :
23 Jun, 2022
Given an integer n, the task is to find the number of trailing zeros in the function f(n)=\prod\limits_{i = 1}^{n} i^{i} i.e. f(n) = 11 * 22 * 33 * ... * nn.
Examples:
Input: n = 5
Output: 5
f(5) = 11 * 22 * 33 * 44 * 55 = 1 * 4 * 27 * 256 * 3125 = 86400000
Input: n = 12
Output: 15
Approach: We know that 5 * 2 = 10 i.e. 1 trailing zero is the result of the multiplication of a single 5 and a single 2. So, if we have x number of 5 and y number of 2 then the number of trailing zeros will be min(x, y).
Now, for every number i in the series, we need to count the number of 2 and 5 in its factors say x and y but the number of 2s and 5s will be x * i and y * i respectively because in the series i is raised to the power itself i.e. ii. Count the number of 2s and 5s in the complete series and print the minimum of them which is the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the number of
// trailing zeros
int trailing_zeros(int N)
{
// To store the number of 2s and 5s
int count_of_two = 0, count_of_five = 0;
for (int i = 1; i <= N; i++) {
int val = i;
while (val % 2 == 0 && val > 0) {
val /= 2;
// If we get a factor 2 then we
// have i number of 2s because
// the power of the number is
// raised to i
count_of_two += i;
}
while (val % 5 == 0 && val > 0) {
val /= 5;
// If we get a factor 5 then
// we have i number of 5s
// because the power of the
// number is raised to i
count_of_five += i;
}
}
// Take the minimum of them
int ans = min(count_of_two, count_of_five);
return ans;
}
// Driver code
int main()
{
int N = 12;
cout << trailing_zeros(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the number of
// trailing zeros
static int trailing_zeros(int N)
{
// To store the number of 2s and 5s
int count_of_two = 0, count_of_five = 0;
for (int i = 1; i <= N; i++)
{
int val = i;
while (val % 2 == 0 && val > 0)
{
val /= 2;
// If we get a factor 2 then we
// have i number of 2s because
// the power of the number is
// raised to i
count_of_two += i;
}
while (val % 5 == 0 && val > 0)
{
val /= 5;
// If we get a factor 5 then
// we have i number of 5s
// because the power of the
// number is raised to i
count_of_five += i;
}
}
// Take the minimum of them
int ans = Math.min(count_of_two, count_of_five);
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 12;
System.out.println(trailing_zeros(N));
}
}
// This code is contributed by chandan_jnu
Python3
# Python 3 implementation of the approach
# Function to return the number of
# trailing zeros
def trailing_zeros(N):
# To store the number of 2s and 5s
count_of_two = 0
count_of_five = 0
for i in range(1, N + 1, 1):
val = i
while (val % 2 == 0 and val > 0):
val /= 2
# If we get a factor 2 then we
# have i number of 2s because
# the power of the number is
# raised to i
count_of_two += i
while (val % 5 == 0 and val > 0):
val /= 5
# If we get a factor 5 then we
# have i number of 5s because
# the power of the number is
# raised to i
count_of_five += i
# Take the minimum of them
ans = min(count_of_two, count_of_five)
return ans
# Driver code
if __name__ == '__main__':
N = 12
print(trailing_zeros(N))
# This code is contributed by
# Sanjit_Prasad
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the number of
// trailing zeros
static int trailing_zeros(int N)
{
// To store the number of 2s and 5s
int count_of_two = 0, count_of_five = 0;
for (int i = 1; i <= N; i++)
{
int val = i;
while (val % 2 == 0 && val > 0)
{
val /= 2;
// If we get a factor 2 then we
// have i number of 2s because
// the power of the number is
// raised to i
count_of_two += i;
}
while (val % 5 == 0 && val > 0)
{
val /= 5;
// If we get a factor 5 then
// we have i number of 5s
// because the power of the
// number is raised to i
count_of_five += i;
}
}
// Take the minimum of them
int ans = Math.Min(count_of_two, count_of_five);
return ans;
}
// Driver code
public static void Main()
{
int N = 12;
Console.WriteLine(trailing_zeros(N));
}
}
// This code is contributed by Ryuga
PHP
<?php
// PHP implementation of the approach
// Function to return the number of
// trailing zeros
function trailing_zeros($N)
{
// To store the number of 2s and 5s
$count_of_two = 0;
$count_of_five = 0;
for ($i = 1; $i <= $N; $i++)
{
$val = $i;
while ($val % 2 == 0 && $val > 0)
{
$val /= 2;
// If we get a factor 2 then we
// have i number of 2s because
// the power of the number is
// raised to i
$count_of_two += $i;
}
while ($val % 5 == 0 && $val > 0)
{
$val /= 5;
// If we get a factor 5 then
// we have i number of 5s
// because the power of the
// number is raised to i
$count_of_five += $i;
}
}
// Take the minimum of them
$ans = min($count_of_two, $count_of_five);
return $ans;
}
// Driver code
$N = 12;
echo trailing_zeros($N);
// This code is contributed by ita_c
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the number of
// trailing zeros
function trailing_zeros(N)
{
// To store the number of 2s and 5s
let count_of_two = 0, count_of_five = 0;
for(let i = 1; i <= N; i++)
{
let val = i;
while (val % 2 == 0 && val > 0)
{
val = parseInt(val / 2);
// If we get a factor 2 then we
// have i number of 2s because
// the power of the number is
// raised to i
count_of_two += i;
}
while (val % 5 == 0 && val > 0)
{
val = parseInt(val / 5);
// If we get a factor 5 then
// we have i number of 5s
// because the power of the
// number is raised to i
count_of_five += i;
}
}
// Take the minimum of them
let ans = Math.min(count_of_two,
count_of_five);
return ans;
}
// Driver code
let N = 12;
document.write(trailing_zeros(N));
// This code is contributed by subhammahato348
</script>
Time Complexity: O(N * (log2N + log5N))
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Number of trailing zeros in N * (N - 2) * (N - 4)*.... Given an integer N, the task is to find the number of trailing zeros in the decimal notation of f(N) where f(N) = 1 if N < 2 and f(N) = N * f(N - 2) if N ? 2 Examples: Input: N = 12 Output: 1 f(12) = 12 * 10 * 8 * 6 * 4 * 2 = 46080 Input: N = 7 Output: 0 Approach: The number of trailing zeros whe
4 min read
Count trailing zeroes in factorial of a number Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5Output: 1 Explanation: Factorial of 5 is 120 which has one trailing 0.Input: n = 20Output: 4Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.Input: n = 100Output: 2
8 min read
Count number of trailing zeros in product of array Given a array size of n, we need to find the total number of zeros in the product of array. Examples: Input : a[] = {100, 20, 40, 25, 4} Output : 6 Product is 100 * 20 * 40 * 25 * 4 which is 8000000 and has 6 trailing 0s. Input : a[] = {10, 100, 20, 30, 25, 4, 43, 25, 50, 90, 12, 80} Output : 13 A s
6 min read
Number of trailing zeroes in base 16 representation of N! Given an integer N, the task is to find the number of trailing zeroes in the base 16 representation of the factorial of N. Examples: Input: N = 6 Output: 1 6! = 720 (base 10) = 2D0 (base 16) Input: N = 100 Output: 24 Approach: Number of trailing zeroes would be the highest power of 16 in the factori
4 min read
Number of trailing zeroes in base B representation of N! Given two positive integers B and N. The task is to find the number of trailing zeroes in b-ary (base B) representation of N! (factorial of N)Examples: Input: N = 5, B = 2 Output: 3 5! = 120 which is represented as 1111000 in base 2. Input: N = 6, B = 9 Output: 1 A naive solution is to find the fact
9 min read