CSES Solutions - Trailing Zeros Last Updated : 02 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Your task is to calculate the number of trailing zeros in the factorial N!. Examples: Input: N = 20Output: 4Explanation: 20! = 2432902008176640000 and it has 4 trailing zeros. Input: N = 6Output: 1Explanation: 6! = 720 and it has 1 trailing zero. Approach: To solve the problem, follow the below idea: If we observe carefully, the number of trailing zeros in N! is same as calculating the number of times the number N! is divisible by 10. We can find this by finding the number of pairs of {2, 5} in the prime factorization of N! as 2 * 5 = 10. Since the number of 2s will always be greater than the number of 5s in prime factorization of N!, therefore we can only calculate the number of 5s to calculate the total number of trailing zeros. Also, the number of 5s in the prime factorization can be different for different numbers. Every multiple of 5 will have at least one 5 in the prime factorization. Similarly, every multiple of 25 will have at least two 5s and every multiple of 125 will have at least three 5s in the prime factorization as so on. So, we can first count the number of 5s for every multiple of 5 then for every multiple of 25, then 125 and so on to calculate the final answer. Step-by-step algorithm: Maintain a function solve(N) to recursively count the number of occurrences of 5 till N.Check if N == 0, then return 0.Otherwise, return N / 5 + solve(N / 5). Below is the implementation of the algorithm: C++ #include <iostream> using namespace std; // Recursive function to calculate the multiples of 5 till N int solve(int N) { if (N == 0) { return 0; } return N / 5 + solve(N / 5); } int main() { int N = 20; cout << solve(N) << "\n"; return 0; } Java import java.util.*; public class Main { // Recursive function to calculate the multiples of 5 till N static int solve(int N) { if (N == 0) { return 0; } return N / 5 + solve(N / 5); } public static void main(String[] args) { int N = 20; System.out.println(solve(N)); } } // This code is contributed by rambabuguphka C# using System; public class GFG { // Recursive function to calculate the multiples of the 5 till N static int Solve(int N) { // Base case: If N is 0 // return 0 if (N == 0) { return 0; } // Recursively calculate the multiples of the 5 and add to the result return N / 5 + Solve(N / 5); } public static void Main(string[] args) { int N = 20; // Call the Solve function and print the result Console.WriteLine(Solve(N)); } } JavaScript // Recursive function to calculate the multiples of 5 till N function solve(N) { if (N === 0) { return 0; } return Math.floor(N / 5) + solve(Math.floor(N / 5)); } function main() { const N = 20; console.log(solve(N)); } main(); Python3 # Recursive function to calculate the multiples of 5 till N def solve(N): if N == 0: return 0 return N // 5 + solve(N // 5) def main(): N = 20 print(solve(N)) if __name__ == "__main__": main() Output4 Time Complexity: O(log5N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article CSES Solutions - Trailing Zeros A alphacozeop Follow Improve Article Tags : Mathematical Competitive Programming DSA Algorithms-Recursion CSES Problems +1 More Practice Tags : Mathematical Similar Reads How Many Zeros in 1 Trillion There are 12 zeros in 1 Trillion. We can numerically represent 1 Trillion as 1,000,000,000,000. Finding the Number of Zeroes in 1 TrillionLet's learn how much exactly is 1 Trillion, and its representation in numbers. No of Zeros in 1 TrillionThere are 12 zeros in one trillion. 1,000,000,000,000 i.e. 3 min read How Many Zeros in a Million? Answer: There are six zeros in 1 million.1 million is written as 1,000,000 in the International numbering system. â1 millionâ is a term that represents ten lakhs in the "Indian Number System". According to the Indian numeral system, 1 million is written as 10,00,000.How Many Zeros in 1 Million?In 1 3 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 trailing zero bits using lookup table Given an integer, count the number of trailing zeroes. For example, for n = 12, its binary representation is 1100 and number of trailing zero bits is 2. Examples : Input : 8 Output : 3 Binary of 8 is 1000, so there are three trailing zero bits. Input : 18 Output : 1 Binary of 18 is 10010, so there i 7 min read How Many Zeros in 10 Million? There are seven zeros in 10 million. 10 million is written as 10,000,000. According to the international numeral system, it is written as 10,000,000.Finding the number of zeroes in 10 millionLet's learn how to find the number of zeroes in 10 million and more.No of Zeros in 10 MillionThere are 7 zero 2 min read Like