-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrailingZeroes.java
More file actions
37 lines (30 loc) · 880 Bytes
/
TrailingZeroes.java
File metadata and controls
37 lines (30 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
public class TrailingZeroes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number n: ");
int n = sc.nextInt();
int low = 0;
int high = 5 * n;
int result = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (counttrailing(mid) >= n) {
result = mid;
high = mid-1;
}
else{
low = mid +1;
}
}
System.out.println("The smallest number whose factorial coontains atleast "+n+" trailing zeroes is: "+result);
sc.close();
}
public static int counttrailing(int mid){
int count = 0;
for(int i =5; mid/i>0;i*=5){
count+=mid/i;
}
return count;
}
}