 # 1 problem 2 solutions

This is in reference to the problem FACTORIAL (Problem code FCTRL)

One of solution got accepted:

``````import java.io.*;
import java.util.*;

public class Main {
public static void main(String [] args) throws IOException{
for (int i = 0; i < Integer.parseInt(num); i++){
int counter = 0;
int div = 5;
while (div <= number){
counter += number/div;
div *= 5;
}
System.out.println(counter);

}
}
}
``````

while the other is giving wrong answer:

``````    import java.util.*;
import java.io.*;
public class Main{

public static void main(String[] args) throws IOException {
int[] a = new int[T];
for(int i=1; i<=T; i++){
a[i-1]=N;}
br.close();
for(int i=0; i<T; i++){
System.out.println(""+zeroes(a[i]));}}

public static int highest(int p, int N){
int result=p;
int count=1;
while(result*p<N){
result*=p;
count++;}
return count;}

public static int zeroes(int N){
int power= highest(5,N);
int result=0;
int num=5;
for(int i=1; i<=power; i++){
result+=N/num;
num*=5;}
return result;}
}
``````

But both solutions are basically the same, just different ways of writing.
Also both give the same result on many inputs I have tried on my compiler.
So why one one of them(first one) got accepted while the other(second one) is giving “wrong answer” according to codechef ?

you missed an equality in your function highest.

it should be

``````while(result*p<=N)
``````

This should get you accepted.

3 Likes

thanks! got it