CSES Solutions - Trailing Zeros Last Updated : 02 Apr, 2024 Summarize Comments Improve Suggest changes Share 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 How Many Zeros in 100 crore? A alphacozeop Follow Improve Article Tags : Competitive Programming Algorithms-Recursion CSES Problems 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 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 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 How Many Zeros in 100 crore? How Many Zeros in 100 Crore?100 crores is equal to 100 times ten million or 1,000 million that is 1 billion.1 Crore = 100 lakhs and 1 lakh has five zeros. This implies, 1 Crore has seven zeroes. Thus, 100 Crore has nine zeros and it is written as 1,00,00,00,000. 100 crore = 1,00,00,00,000 (9 Zeroes) 4 min read How Many Zeros in 1 Centillion? There are 303 zeros in one centillion as it is represented as 10303. Centillion is one of the larger cardinal numbers in mathematics, which helps us grasp the scale of the universe as there are fewer atoms in the universe than one centillion. In the list of hundreds, thousands, millions, billions, e 2 min read Like