Numbers whose factorials end with n zeros
Last Updated :
14 Feb, 2023
Given an integer n, we need to find the number of positive integers whose factorial ends with n zeros.
Examples:
Input : n = 1
Output : 5 6 7 8 9
Explanation: Here, 5! = 120, 6! = 720,
7! = 5040, 8! = 40320 and 9! = 362880.
Input : n = 2
Output : 10 11 12 13 14
Prerequisite : Trailing zeros in factorial.
Naive approach:We can just iterate through the range of integers and find the number of trailing zeros of all the numbers and print the numbers with n trailing zeros.
Efficient Approach:In this approach we use binary search. Use binary search for all the numbers in the range and get the first number with n trailing zeros. Find all the numbers with m trailing zeros after that number.
C++
#include <bits/stdc++.h>
using namespace std;
int trailingZeroes( int n)
{
int cnt = 0;
while (n > 0) {
n /= 5;
cnt += n;
}
return cnt;
}
void binarySearch( int n)
{
int low = 0;
int high = 1e6;
while (low < high) {
int mid = (low + high) / 2;
int count = trailingZeroes(mid);
if (count < n)
low = mid + 1;
else
high = mid;
}
vector< int > result;
while (trailingZeroes(low) == n) {
result.push_back(low);
low++;
}
for ( int i = 0; i < result.size(); i++)
cout << result[i] << " " ;
}
int main()
{
int n = 2;
binarySearch(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int trailingZeroes( int n)
{
int cnt = 0 ;
while (n > 0 )
{
n /= 5 ;
cnt += n;
}
return cnt;
}
static void binarySearch( int n)
{
int low = 0 ;
int high = 1000000 ;
while (low < high) {
int mid = (low + high) / 2 ;
int count = trailingZeroes(mid);
if (count < n)
low = mid + 1 ;
else
high = mid;
}
int result[] = new int [ 1000 ];
int k = 0 ;
while (trailingZeroes(low) == n) {
result[k] = low;
k++;
low++;
}
for ( int i = 0 ; i < k; i++)
System.out.print(result[i] + " " );
}
public static void main(String args[])
{
int n = 3 ;
binarySearch(n);
}
}
|
Python3
def trailingZeroes( n ):
cnt = 0
while n > 0 :
n = int (n / 5 )
cnt + = n
return cnt
def binarySearch( n ):
low = 0
high = 1e6
while low < high:
mid = int ((low + high) / 2 )
count = trailingZeroes(mid)
if count < n:
low = mid + 1
else :
high = mid
result = list ()
while trailingZeroes(low) = = n:
result.append(low)
low + = 1
for i in range ( len (result)):
print (result[i],end = " " )
n = 2
binarySearch(n)
|
C#
using System;
class GFG {
static int trailingZeroes( int n)
{
int cnt = 0;
while (n > 0)
{
n /= 5;
cnt += n;
}
return cnt;
}
static void binarySearch( int n)
{
int low = 0;
int high = 1000000;
while (low < high) {
int mid = (low + high) / 2;
int count = trailingZeroes(mid);
if (count < n)
low = mid + 1;
else
high = mid;
}
int []result = new int [1000];
int k = 0;
while (trailingZeroes(low) == n) {
result[k] = low;
k++;
low++;
}
for ( int i = 0; i < k; i++)
Console.Write(result[i] + " " );
}
public static void Main()
{
int n = 2;
binarySearch(n);
}
}
|
PHP
<?php
function trailingZeroes( $n )
{
$cnt = 0;
while ( $n > 0)
{
$n = intval ( $n / 5);
$cnt += $n ;
}
return $cnt ;
}
function binarySearch( $n )
{
$low = 0;
$high = 1e6;
while ( $low < $high )
{
$mid = intval (( $low + $high ) / 2);
$count = trailingZeroes( $mid );
if ( $count < $n )
$low = $mid + 1;
else
$high = $mid ;
}
$result = array ();
while (trailingZeroes( $low ) == $n )
{
array_push ( $result , $low );
$low ++;
}
for ( $i = 0;
$i < sizeof( $result ); $i ++)
echo $result [ $i ] . " " ;
}
$n = 2;
binarySearch( $n );
?>
|
Javascript
<script>
function trailingZeroes(n)
{
var cnt = 0;
while (n > 0) {
n = parseInt(n/5);
cnt += n;
}
return cnt;
}
function binarySearch(n)
{
var low = 0;
var high = 1e6;
while (low < high) {
var mid = parseInt((low + high) / 2);
var count = trailingZeroes(mid);
if (count < n)
low = mid + 1;
else
high = mid;
}
var result = [];
while (trailingZeroes(low) == n) {
result.push(low);
low++;
}
for ( var i = 0; i < result.length; i++)
document.write( result[i] + " " );
}
var n = 2;
binarySearch(n);
</script>
|
Output:
10 11 12 13 14
Time Complexity: O(log(m)). Here m=1e6
Auxiliary Space: O(k) where k is the number of trailing zeros.
Share your thoughts in the comments
Please Login to comment...