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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read