small factorial help..

import java.math.BigInteger;
import java.util.Scanner;

class fctrl {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		
		Scanner sc=new Scanner(System.in);
		int t;
		
		t=sc.nextInt();
		BigInteger n[] =new BigInteger[t];
		for(int i=0;i<t;i++)
		{
			n[i]=sc.nextBigInteger();
		}
		BigInteger f;
		BigInteger d;
		int c=0;
		for(int i=0;i<t;i++)
		{
			f=fact(n[i]);
			System.out.println("fact="+f);
			d=f.mod(BigInteger.valueOf(10));
			f=f.divide(BigInteger.valueOf(10));
			c=0;
			while(d.compareTo(BigInteger.ZERO)==0)
			{
				c++;
				d=f.mod(BigInteger.valueOf(10));
				f=f.divide(BigInteger.valueOf(10));
				
			}
			System.out.println(c);
		}
		
		
		
		
	}

	private static BigInteger fact(BigInteger l) {
		
		
		
		
		
		//System.out.println(l);
		
		if(l.compareTo(BigInteger.ONE)==0)
			return BigInteger.valueOf(1);
		else
			return l.multiply(fact(l.subtract(BigInteger.valueOf(1))));

	}

}

i read the tutorial given for the question i understood what it said but i want to know why my program gives a wrong answer because BigInteger is big enough to handle those many long digits as i tried the factorial of hundred and it gave me the correct answer so where is the wrong answer coming from?

Why are you printing

System.out.println("fact="+f);

?

read more - http://www.codechef.com/wiki/faq#How_does_Codechef_test_whether_my_solution_is_correct_or_not

2 Likes

extremely sorry!!! :frowning:
i posted the wrong code!
will create a new question very sorry once again

No, please use edit link :wink:

Here is your last submission, you are submitting code for another problem AFAIK

You should read faq for time limit exceeded because Scanner is very slow and so is System.out.println(). This post and this tutorial can help you in that. It may work for this problem but it will be very hard to get accepted in future problems using that IO method.

1 Like
//