简体   繁体   中英

What should I do if I am finding the factorial of a big Number in an efficient way still I am getting a TLE?

Question: Extra Long Factorials( https://www.hackerrank.com/challenges/extra-long-factorials/problem ) constraints: 1<n<=100

I am doing in an efficient way plus its the same solution available in net still when I am submitting the code its showing TLE. Below is my code

public static void multiply(ArrayList<Integer> prod,int i)
    {
        int carry=0;
        for(int j=0;j<prod.size();j++)
        {
            int currp=prod.get(j)*i+carry;
            prod.add(j,currp%10);
            carry=currp/10;
        }
        while(carry>0)
        {
            prod.add(carry%10);
            carry/=10;
        }
    }
    // Complete the extraLongFactorials function below.
    static void extraLongFactorials(int n) {

        ArrayList<Integer> prod= new ArrayList<>();
        prod.add(1);
        for(int i=2;i<n;i++)
        multiply(prod,i);
        
        for(int i=prod.size()-1;i>=0;i--)
        System.out.print(prod.get(i));
        
        System.out.println();
    }

Java has built-in classes that provide arbitrary precision. Use BigInteger.valueOf(long) and BigInteger.multiply(BigInteger) like

static void extraLongFactorials(int n) {
    BigInteger v = BigInteger.valueOf(n);
    while (--n > 0) {
        v = v.multiply(BigInteger.valueOf(n));
    }
    System.out.println(v);
}

Your code has some serious bugs.

I tested it be pasting it into a class and adding a main method:

public static void main(String[] args) {
    for (int i = 1; i <= 10; i++) {
        extraLongFactorials(i);
    }
}
  • Your code returns the correct result for the factorial of 1 .
  • Your code returns "1" as the factorial of 2 , which is wrong (it should be "2")
  • Your code cannot compute the factorial of 3 - it never stops

I intentionally don't correct your mistakes - try to find and fix them yourself (using a debugger).

The good news: there are two bugs in your code. Fix these and your code will pass the tests.

So I made 2 mistakes in my code. Now my code got submitted. Below is the correct code-

public static void multiply(ArrayList<Integer> prod,int i)
    {
        int carry=0;
        for(int j=0;j<prod.size();j++)
        {
            int currp=prod.get(j)*i+carry;
            prod.set(j,currp%10);
            carry=currp/10;
        }
        while(carry>0)
        {
            prod.add(carry%10);
            carry/=10;
        }
    }
    // Complete the extraLongFactorials function below.
    static void extraLongFactorials(int n) {

        ArrayList<Integer> prod= new ArrayList<>();
        prod.add(1);
        for(int i=1;i<=n;i++)
        multiply(prod,i);
        
        for(int i=prod.size()-1;i>=0;i--)
        System.out.print(prod.get(i));
        
        System.out.println();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM