Hello,
My code for the classical problem factorial shows TLE when I submit it. Upon further investigation, I found that my code runs the upper limit 1000000000 in around 13 seconds. However, the time limit for the problem is 6 seconds. I would appreciate any helpful tips on making my code more efficient. I apologize if the code is sorta messy, I'm a beginner to programming in general.
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
long[] input = new long[t];
long n;
double counter;
double product;
for (int j = 0; j < t; j++) {
input[j] = scanner.nextLong();
}
for (int i = 0; i < t; i++) {
n = input[i];
counter = 0;
product = 1L;
for (long f = 1L; f <= n; f = f + 1L) {
product = product * f;
}
for (long z =5L; n / z >= 1; z = z * 5) {
counter = counter + Math.floor(n / z);
}
System.out.println((int)counter);
}
}
}