This is a solution to the CodeHhef problem FCTRL2 where we are supposed to find the factorial of numbers up to 100. I have tried several techniques but I am always exceeding their time limit (1 second). Please help me optimize this code to make it execute faster.    import java.math.BigInteger;    import java.io.*;    class Abc{        public static void main(String[] args) throws IOException    {        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));        BigInteger fact=new BigInteger(1);       // BigInteger inc=new BigInteger(1);        String str=br.readLine();        int n=Integer.parseInt(str);        for(int i=0;i<n;i++)        {    String st=br.readLine();            BigInteger number=new BigInteger(st);            for(BigInteger bi = BigInteger.valueOf(1);bi.compareTo(number)>=0;bi = bi.add(BigInteger.ONE))            {                fact=fact.multiply(bi);             }            System.out.println(fact);        }    }    }
Optimization of Factorial Function
java;programming challenge;time limit exceeded