Find factorial of large numbers in Java












6















I tried to find the factorial of a large number e.g. 8785856 in a typical way using for-loop and double data type.



But it is displaying infinity as the result, may be because it is exceeding its limit.



So please guide me the way to find the factorial of a very large number.



My code:



class abc
{
public static void main (Stringargs)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}

System.out.println(fact);
}
}


Output:-



Infinity


I am new to Java but have learned some concepts of IO-handling and all.










share|improve this question

























  • Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

    – Lion
    Jul 12 '12 at 7:31













  • you loop variable i is int and you are trying to store a huge number in it, try making it a double too

    – Shades88
    Jul 12 '12 at 7:33











  • According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

    – Zéychin
    Jul 12 '12 at 7:39













  • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:03











  • What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

    – Louis Wasserman
    Jul 12 '12 at 9:39
















6















I tried to find the factorial of a large number e.g. 8785856 in a typical way using for-loop and double data type.



But it is displaying infinity as the result, may be because it is exceeding its limit.



So please guide me the way to find the factorial of a very large number.



My code:



class abc
{
public static void main (Stringargs)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}

System.out.println(fact);
}
}


Output:-



Infinity


I am new to Java but have learned some concepts of IO-handling and all.










share|improve this question

























  • Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

    – Lion
    Jul 12 '12 at 7:31













  • you loop variable i is int and you are trying to store a huge number in it, try making it a double too

    – Shades88
    Jul 12 '12 at 7:33











  • According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

    – Zéychin
    Jul 12 '12 at 7:39













  • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:03











  • What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

    – Louis Wasserman
    Jul 12 '12 at 9:39














6












6








6


4






I tried to find the factorial of a large number e.g. 8785856 in a typical way using for-loop and double data type.



But it is displaying infinity as the result, may be because it is exceeding its limit.



So please guide me the way to find the factorial of a very large number.



My code:



class abc
{
public static void main (Stringargs)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}

System.out.println(fact);
}
}


Output:-



Infinity


I am new to Java but have learned some concepts of IO-handling and all.










share|improve this question
















I tried to find the factorial of a large number e.g. 8785856 in a typical way using for-loop and double data type.



But it is displaying infinity as the result, may be because it is exceeding its limit.



So please guide me the way to find the factorial of a very large number.



My code:



class abc
{
public static void main (Stringargs)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}

System.out.println(fact);
}
}


Output:-



Infinity


I am new to Java but have learned some concepts of IO-handling and all.







java factorial largenumber






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '12 at 16:30









Bill the Lizard

296k158500792




296k158500792










asked Jul 12 '12 at 7:29









Himanshu AggarwalHimanshu Aggarwal

1,08611629




1,08611629













  • Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

    – Lion
    Jul 12 '12 at 7:31













  • you loop variable i is int and you are trying to store a huge number in it, try making it a double too

    – Shades88
    Jul 12 '12 at 7:33











  • According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

    – Zéychin
    Jul 12 '12 at 7:39













  • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:03











  • What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

    – Louis Wasserman
    Jul 12 '12 at 9:39



















  • Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

    – Lion
    Jul 12 '12 at 7:31













  • you loop variable i is int and you are trying to store a huge number in it, try making it a double too

    – Shades88
    Jul 12 '12 at 7:33











  • According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

    – Zéychin
    Jul 12 '12 at 7:39













  • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:03











  • What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

    – Louis Wasserman
    Jul 12 '12 at 9:39

















Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

– Lion
Jul 12 '12 at 7:31







Why don't you use higher data type like BigInteger and also is this homework? If yes then tag as such.

– Lion
Jul 12 '12 at 7:31















you loop variable i is int and you are trying to store a huge number in it, try making it a double too

– Shades88
Jul 12 '12 at 7:33





you loop variable i is int and you are trying to store a huge number in it, try making it a double too

– Shades88
Jul 12 '12 at 7:33













According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

– Zéychin
Jul 12 '12 at 7:39







According to my calculations, it will take around 25 megabytes of memory in order to represent this number. and even then, that would be without the overheard of the BigInteger class.

– Zéychin
Jul 12 '12 at 7:39















yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

– Himanshu Aggarwal
Jul 12 '12 at 8:03





yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

– Himanshu Aggarwal
Jul 12 '12 at 8:03













What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

– Louis Wasserman
Jul 12 '12 at 9:39





What do you mean, "fails to display the answer"? Can you post the code that "failed to display the answer"?

– Louis Wasserman
Jul 12 '12 at 9:39












13 Answers
13






active

oldest

votes


















8














public static void main(String args) {
BigInteger fact = BigInteger.valueOf(1);
for (int i = 1; i <= 8785856; i++)
fact = fact.multiply(BigInteger.valueOf(i));
System.out.println(fact);
}





share|improve this answer
























  • actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

    – Himanshu Aggarwal
    Jul 12 '12 at 8:12











  • yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

    – manurajhada
    Jul 12 '12 at 8:30











  • but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:38













  • you can increase your jvm memory

    – manurajhada
    Jul 12 '12 at 8:39











  • how can i do this??? plz tell me...

    – Himanshu Aggarwal
    Jul 12 '12 at 14:43





















9














You might want to reconsider calculating this huge value. Wolfram Alpha's Approximation suggests it will most certainly not fit in your main memory to be displayed.






share|improve this answer
























  • +1 For the link - it's COOL!

    – Bohemian
    Jul 12 '12 at 7:41











  • thanx for this link man...:-)

    – Himanshu Aggarwal
    Jul 12 '12 at 8:08











  • Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

    – Otto Allmendinger
    Jul 12 '12 at 8:30











  • thanx man.....by the way it wud be too long...it might crash.....:D

    – Himanshu Aggarwal
    Jul 12 '12 at 8:37











  • I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

    – ThePadawan
    Jul 12 '12 at 14:40



















5














This code should work fine :-



public class BigMath {
public static String factorial(int n) {
return factorial(n, 300);
}

private static String factorial(int n, int maxSize) {
int res = new int[maxSize];
res[0] = 1; // Initialize result
int res_size = 1;

// Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
for (int x = 2; x <= n; x++) {
res_size = multiply(x, res, res_size);
}

StringBuffer buff = new StringBuffer();
for (int i = res_size - 1; i >= 0; i--) {
buff.append(res[i]);
}

return buff.toString();
}

/**
* This function multiplies x with the number represented by res. res_size
* is size of res or number of digits in the number represented by res.
* This function uses simple school mathematics for multiplication.
*
* This function may value of res_size and returns the new value of res_size.
*/
private static int multiply(int x, int res, int res_size) {
int carry = 0; // Initialize carry.

// One by one multiply n with individual digits of res.
for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of 'prod' in res
carry = prod / 10; // Put rest in carry
}

// Put carry in res and increase result size.
while (carry != 0) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}

return res_size;
}

/** Driver method. */
public static void main(String args) {
int n = 100;

System.out.printf("Factorial %d = %s%n", n, factorial(n));
}
}





share|improve this answer


























  • This actually is a better program, efficient, no use of BigInteger.

    – Shubham A.
    Nov 9 '16 at 9:54



















3














Hint: Use the BigInteger class, and be prepared to give the JVM a lot of memory. The value of 8785856! is a really big number.






share|improve this answer
























  • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:00











  • Try increasing the heap size.

    – Stephen C
    Jul 12 '12 at 10:01



















1














Use the class BigInteger. ( I am not sure if that will even work for such huge integers )






share|improve this answer
























  • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:01











  • @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

    – gaganbm
    Jul 12 '12 at 8:06













  • yep...so can u explain me this in a bit more detail.

    – Himanshu Aggarwal
    Jul 12 '12 at 8:09











  • That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

    – gaganbm
    Jul 12 '12 at 8:14











  • okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

    – Himanshu Aggarwal
    Jul 12 '12 at 8:16



















1














This blog post explains biginteger factorial in java with examples.






share|improve this answer
























  • yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

    – Himanshu Aggarwal
    Jul 12 '12 at 7:59



















0














Infinity is a special reserved value in the Double class used when you have exceed the maximum number the a double can hold.



If you want your code to work, use the BigDecimal class, but given the input number, don't expect your program to finish execution any time soon.






share|improve this answer
























  • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

    – Himanshu Aggarwal
    Jul 12 '12 at 8:01



















0














The above solutions for your problem (8785856!) using BigInteger would take literally hours of CPU time if not days. Do you need the exact result or would an approximation suffice?



There is a mathematical approach called "Sterling's Approximation
" which can be computed simply and fast, and the following is Gosper's improvement:
enter image description here






share|improve this answer































    0














     import java.util.*;
    import java.math.*;

    class main
    {
    public static void main(String args)
    {
    Scanner sc= new Scanner(System.in);

    int i;
    int n=sc.nextInt();


    BigInteger fact = BigInteger.valueOf(1);

    for ( i = 1; i <= n; i++)
    {
    fact = fact.multiply(BigInteger.valueOf(i));
    }
    System.out.println(fact);

    }
    }





    share|improve this answer
























    • I think the answer would be better without the scanner class.

      – MaxPower
      Aug 7 '17 at 19:21











    • where you declare or defined multiply????

      – Ved Prakash
      Aug 7 '17 at 19:48



















    0














    Try this:



    import java.math.BigInteger;

    public class LargeFactorial
    {
    public static void main(String args)
    {
    int n = 50;
    }
    public static BigInteger factorial(int n)
    {
    BigInteger result = BigInteger.ONE;
    for (int i = 1; i <= n; i++)
    result = result.multiply(new BigInteger(i + ""));
    return result;
    }
    }





    share|improve this answer
























    • Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

      – Gautam Sarkar
      Jun 10 '18 at 3:31



















    0














        Scanner r = new Scanner(System.in);
    System.out.print("Input Number : ");
    int num = r.nextInt();
    int ans = 1;
    if (num <= 0) {
    ans = 0;
    }
    while (num > 0) {
    System.out.println(num + " x ");
    ans *= num--;
    }
    System.out.println("bb=" + ans);





    share|improve this answer
























    • edit your answer and add short explanation to your code

      – barbsan
      Nov 21 '18 at 11:21



















    -1














    To really find out the factorial of this number you should use PYTHON functions , and try to open the task manager and see how much memory the compiler takes .
    After that you will know that how much time JVM is going to take ,as PYTHON is the best language for numerical calculations.






    share|improve this answer































      -1














      import java.util.Scanner;


      public class factorial {
      public static void main(String args) {
      System.out.println("Enter the number : ");
      Scanner s=new Scanner(System.in);
      int n=s.nextInt();
      factorial f=new factorial();
      int result=f.fact(n);
      System.out.println("factorial of "+n+" is "+result);
      }
      int fact(int a)
      {
      if(a==1)
      return 1;
      else
      return a*fact(a-1);
      }

      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11446973%2ffind-factorial-of-large-numbers-in-java%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        13 Answers
        13






        active

        oldest

        votes








        13 Answers
        13






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8














        public static void main(String args) {
        BigInteger fact = BigInteger.valueOf(1);
        for (int i = 1; i <= 8785856; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
        System.out.println(fact);
        }





        share|improve this answer
























        • actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

          – Himanshu Aggarwal
          Jul 12 '12 at 8:12











        • yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

          – manurajhada
          Jul 12 '12 at 8:30











        • but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:38













        • you can increase your jvm memory

          – manurajhada
          Jul 12 '12 at 8:39











        • how can i do this??? plz tell me...

          – Himanshu Aggarwal
          Jul 12 '12 at 14:43


















        8














        public static void main(String args) {
        BigInteger fact = BigInteger.valueOf(1);
        for (int i = 1; i <= 8785856; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
        System.out.println(fact);
        }





        share|improve this answer
























        • actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

          – Himanshu Aggarwal
          Jul 12 '12 at 8:12











        • yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

          – manurajhada
          Jul 12 '12 at 8:30











        • but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:38













        • you can increase your jvm memory

          – manurajhada
          Jul 12 '12 at 8:39











        • how can i do this??? plz tell me...

          – Himanshu Aggarwal
          Jul 12 '12 at 14:43
















        8












        8








        8







        public static void main(String args) {
        BigInteger fact = BigInteger.valueOf(1);
        for (int i = 1; i <= 8785856; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
        System.out.println(fact);
        }





        share|improve this answer













        public static void main(String args) {
        BigInteger fact = BigInteger.valueOf(1);
        for (int i = 1; i <= 8785856; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
        System.out.println(fact);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:50









        manurajhadamanurajhada

        4,23421739




        4,23421739













        • actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

          – Himanshu Aggarwal
          Jul 12 '12 at 8:12











        • yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

          – manurajhada
          Jul 12 '12 at 8:30











        • but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:38













        • you can increase your jvm memory

          – manurajhada
          Jul 12 '12 at 8:39











        • how can i do this??? plz tell me...

          – Himanshu Aggarwal
          Jul 12 '12 at 14:43





















        • actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

          – Himanshu Aggarwal
          Jul 12 '12 at 8:12











        • yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

          – manurajhada
          Jul 12 '12 at 8:30











        • but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:38













        • you can increase your jvm memory

          – manurajhada
          Jul 12 '12 at 8:39











        • how can i do this??? plz tell me...

          – Himanshu Aggarwal
          Jul 12 '12 at 14:43



















        actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

        – Himanshu Aggarwal
        Jul 12 '12 at 8:12





        actually i have tried this one out but i am waiting for the compiler for a long time to execute this......

        – Himanshu Aggarwal
        Jul 12 '12 at 8:12













        yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

        – manurajhada
        Jul 12 '12 at 8:30





        yeah you have to. and it will take lots of jvm memory also but we have no alternate as the factorial number is too big !!

        – manurajhada
        Jul 12 '12 at 8:30













        but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:38







        but i waited for atleast 30-35 mins... but nothing happened.... should i wait for for time the next time i run it???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:38















        you can increase your jvm memory

        – manurajhada
        Jul 12 '12 at 8:39





        you can increase your jvm memory

        – manurajhada
        Jul 12 '12 at 8:39













        how can i do this??? plz tell me...

        – Himanshu Aggarwal
        Jul 12 '12 at 14:43







        how can i do this??? plz tell me...

        – Himanshu Aggarwal
        Jul 12 '12 at 14:43















        9














        You might want to reconsider calculating this huge value. Wolfram Alpha's Approximation suggests it will most certainly not fit in your main memory to be displayed.






        share|improve this answer
























        • +1 For the link - it's COOL!

          – Bohemian
          Jul 12 '12 at 7:41











        • thanx for this link man...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:08











        • Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

          – Otto Allmendinger
          Jul 12 '12 at 8:30











        • thanx man.....by the way it wud be too long...it might crash.....:D

          – Himanshu Aggarwal
          Jul 12 '12 at 8:37











        • I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

          – ThePadawan
          Jul 12 '12 at 14:40
















        9














        You might want to reconsider calculating this huge value. Wolfram Alpha's Approximation suggests it will most certainly not fit in your main memory to be displayed.






        share|improve this answer
























        • +1 For the link - it's COOL!

          – Bohemian
          Jul 12 '12 at 7:41











        • thanx for this link man...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:08











        • Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

          – Otto Allmendinger
          Jul 12 '12 at 8:30











        • thanx man.....by the way it wud be too long...it might crash.....:D

          – Himanshu Aggarwal
          Jul 12 '12 at 8:37











        • I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

          – ThePadawan
          Jul 12 '12 at 14:40














        9












        9








        9







        You might want to reconsider calculating this huge value. Wolfram Alpha's Approximation suggests it will most certainly not fit in your main memory to be displayed.






        share|improve this answer













        You might want to reconsider calculating this huge value. Wolfram Alpha's Approximation suggests it will most certainly not fit in your main memory to be displayed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:35









        ThePadawanThePadawan

        584417




        584417













        • +1 For the link - it's COOL!

          – Bohemian
          Jul 12 '12 at 7:41











        • thanx for this link man...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:08











        • Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

          – Otto Allmendinger
          Jul 12 '12 at 8:30











        • thanx man.....by the way it wud be too long...it might crash.....:D

          – Himanshu Aggarwal
          Jul 12 '12 at 8:37











        • I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

          – ThePadawan
          Jul 12 '12 at 14:40



















        • +1 For the link - it's COOL!

          – Bohemian
          Jul 12 '12 at 7:41











        • thanx for this link man...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:08











        • Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

          – Otto Allmendinger
          Jul 12 '12 at 8:30











        • thanx man.....by the way it wud be too long...it might crash.....:D

          – Himanshu Aggarwal
          Jul 12 '12 at 8:37











        • I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

          – ThePadawan
          Jul 12 '12 at 14:40

















        +1 For the link - it's COOL!

        – Bohemian
        Jul 12 '12 at 7:41





        +1 For the link - it's COOL!

        – Bohemian
        Jul 12 '12 at 7:41













        thanx for this link man...:-)

        – Himanshu Aggarwal
        Jul 12 '12 at 8:08





        thanx for this link man...:-)

        – Himanshu Aggarwal
        Jul 12 '12 at 8:08













        Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

        – Otto Allmendinger
        Jul 12 '12 at 8:30





        Unless I did the calculation wrong, it would fit into memory: log2(10^10^8) = 3.32 * 10^8 = 300M bits = 38MByte

        – Otto Allmendinger
        Jul 12 '12 at 8:30













        thanx man.....by the way it wud be too long...it might crash.....:D

        – Himanshu Aggarwal
        Jul 12 '12 at 8:37





        thanx man.....by the way it wud be too long...it might crash.....:D

        – Himanshu Aggarwal
        Jul 12 '12 at 8:37













        I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

        – ThePadawan
        Jul 12 '12 at 14:40





        I have to say, I was too lazy to calculate the memory usage myself. Out of curiosity, does any one know what would happen if you tried to print a 38MB chunk of memory?

        – ThePadawan
        Jul 12 '12 at 14:40











        5














        This code should work fine :-



        public class BigMath {
        public static String factorial(int n) {
        return factorial(n, 300);
        }

        private static String factorial(int n, int maxSize) {
        int res = new int[maxSize];
        res[0] = 1; // Initialize result
        int res_size = 1;

        // Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
        for (int x = 2; x <= n; x++) {
        res_size = multiply(x, res, res_size);
        }

        StringBuffer buff = new StringBuffer();
        for (int i = res_size - 1; i >= 0; i--) {
        buff.append(res[i]);
        }

        return buff.toString();
        }

        /**
        * This function multiplies x with the number represented by res. res_size
        * is size of res or number of digits in the number represented by res.
        * This function uses simple school mathematics for multiplication.
        *
        * This function may value of res_size and returns the new value of res_size.
        */
        private static int multiply(int x, int res, int res_size) {
        int carry = 0; // Initialize carry.

        // One by one multiply n with individual digits of res.
        for (int i = 0; i < res_size; i++) {
        int prod = res[i] * x + carry;
        res[i] = prod % 10; // Store last digit of 'prod' in res
        carry = prod / 10; // Put rest in carry
        }

        // Put carry in res and increase result size.
        while (carry != 0) {
        res[res_size] = carry % 10;
        carry = carry / 10;
        res_size++;
        }

        return res_size;
        }

        /** Driver method. */
        public static void main(String args) {
        int n = 100;

        System.out.printf("Factorial %d = %s%n", n, factorial(n));
        }
        }





        share|improve this answer


























        • This actually is a better program, efficient, no use of BigInteger.

          – Shubham A.
          Nov 9 '16 at 9:54
















        5














        This code should work fine :-



        public class BigMath {
        public static String factorial(int n) {
        return factorial(n, 300);
        }

        private static String factorial(int n, int maxSize) {
        int res = new int[maxSize];
        res[0] = 1; // Initialize result
        int res_size = 1;

        // Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
        for (int x = 2; x <= n; x++) {
        res_size = multiply(x, res, res_size);
        }

        StringBuffer buff = new StringBuffer();
        for (int i = res_size - 1; i >= 0; i--) {
        buff.append(res[i]);
        }

        return buff.toString();
        }

        /**
        * This function multiplies x with the number represented by res. res_size
        * is size of res or number of digits in the number represented by res.
        * This function uses simple school mathematics for multiplication.
        *
        * This function may value of res_size and returns the new value of res_size.
        */
        private static int multiply(int x, int res, int res_size) {
        int carry = 0; // Initialize carry.

        // One by one multiply n with individual digits of res.
        for (int i = 0; i < res_size; i++) {
        int prod = res[i] * x + carry;
        res[i] = prod % 10; // Store last digit of 'prod' in res
        carry = prod / 10; // Put rest in carry
        }

        // Put carry in res and increase result size.
        while (carry != 0) {
        res[res_size] = carry % 10;
        carry = carry / 10;
        res_size++;
        }

        return res_size;
        }

        /** Driver method. */
        public static void main(String args) {
        int n = 100;

        System.out.printf("Factorial %d = %s%n", n, factorial(n));
        }
        }





        share|improve this answer


























        • This actually is a better program, efficient, no use of BigInteger.

          – Shubham A.
          Nov 9 '16 at 9:54














        5












        5








        5







        This code should work fine :-



        public class BigMath {
        public static String factorial(int n) {
        return factorial(n, 300);
        }

        private static String factorial(int n, int maxSize) {
        int res = new int[maxSize];
        res[0] = 1; // Initialize result
        int res_size = 1;

        // Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
        for (int x = 2; x <= n; x++) {
        res_size = multiply(x, res, res_size);
        }

        StringBuffer buff = new StringBuffer();
        for (int i = res_size - 1; i >= 0; i--) {
        buff.append(res[i]);
        }

        return buff.toString();
        }

        /**
        * This function multiplies x with the number represented by res. res_size
        * is size of res or number of digits in the number represented by res.
        * This function uses simple school mathematics for multiplication.
        *
        * This function may value of res_size and returns the new value of res_size.
        */
        private static int multiply(int x, int res, int res_size) {
        int carry = 0; // Initialize carry.

        // One by one multiply n with individual digits of res.
        for (int i = 0; i < res_size; i++) {
        int prod = res[i] * x + carry;
        res[i] = prod % 10; // Store last digit of 'prod' in res
        carry = prod / 10; // Put rest in carry
        }

        // Put carry in res and increase result size.
        while (carry != 0) {
        res[res_size] = carry % 10;
        carry = carry / 10;
        res_size++;
        }

        return res_size;
        }

        /** Driver method. */
        public static void main(String args) {
        int n = 100;

        System.out.printf("Factorial %d = %s%n", n, factorial(n));
        }
        }





        share|improve this answer















        This code should work fine :-



        public class BigMath {
        public static String factorial(int n) {
        return factorial(n, 300);
        }

        private static String factorial(int n, int maxSize) {
        int res = new int[maxSize];
        res[0] = 1; // Initialize result
        int res_size = 1;

        // Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
        for (int x = 2; x <= n; x++) {
        res_size = multiply(x, res, res_size);
        }

        StringBuffer buff = new StringBuffer();
        for (int i = res_size - 1; i >= 0; i--) {
        buff.append(res[i]);
        }

        return buff.toString();
        }

        /**
        * This function multiplies x with the number represented by res. res_size
        * is size of res or number of digits in the number represented by res.
        * This function uses simple school mathematics for multiplication.
        *
        * This function may value of res_size and returns the new value of res_size.
        */
        private static int multiply(int x, int res, int res_size) {
        int carry = 0; // Initialize carry.

        // One by one multiply n with individual digits of res.
        for (int i = 0; i < res_size; i++) {
        int prod = res[i] * x + carry;
        res[i] = prod % 10; // Store last digit of 'prod' in res
        carry = prod / 10; // Put rest in carry
        }

        // Put carry in res and increase result size.
        while (carry != 0) {
        res[res_size] = carry % 10;
        carry = carry / 10;
        res_size++;
        }

        return res_size;
        }

        /** Driver method. */
        public static void main(String args) {
        int n = 100;

        System.out.printf("Factorial %d = %s%n", n, factorial(n));
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 21 '16 at 15:33









        Mr. Polywhirl

        17.5k85092




        17.5k85092










        answered Jul 26 '15 at 11:59









        KhushiKhushi

        359514




        359514













        • This actually is a better program, efficient, no use of BigInteger.

          – Shubham A.
          Nov 9 '16 at 9:54



















        • This actually is a better program, efficient, no use of BigInteger.

          – Shubham A.
          Nov 9 '16 at 9:54

















        This actually is a better program, efficient, no use of BigInteger.

        – Shubham A.
        Nov 9 '16 at 9:54





        This actually is a better program, efficient, no use of BigInteger.

        – Shubham A.
        Nov 9 '16 at 9:54











        3














        Hint: Use the BigInteger class, and be prepared to give the JVM a lot of memory. The value of 8785856! is a really big number.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:00











        • Try increasing the heap size.

          – Stephen C
          Jul 12 '12 at 10:01
















        3














        Hint: Use the BigInteger class, and be prepared to give the JVM a lot of memory. The value of 8785856! is a really big number.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:00











        • Try increasing the heap size.

          – Stephen C
          Jul 12 '12 at 10:01














        3












        3








        3







        Hint: Use the BigInteger class, and be prepared to give the JVM a lot of memory. The value of 8785856! is a really big number.






        share|improve this answer













        Hint: Use the BigInteger class, and be prepared to give the JVM a lot of memory. The value of 8785856! is a really big number.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:33









        Stephen CStephen C

        524k72584942




        524k72584942













        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:00











        • Try increasing the heap size.

          – Stephen C
          Jul 12 '12 at 10:01



















        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:00











        • Try increasing the heap size.

          – Stephen C
          Jul 12 '12 at 10:01

















        yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:00





        yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:00













        Try increasing the heap size.

        – Stephen C
        Jul 12 '12 at 10:01





        Try increasing the heap size.

        – Stephen C
        Jul 12 '12 at 10:01











        1














        Use the class BigInteger. ( I am not sure if that will even work for such huge integers )






        share|improve this answer
























        • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01











        • @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

          – gaganbm
          Jul 12 '12 at 8:06













        • yep...so can u explain me this in a bit more detail.

          – Himanshu Aggarwal
          Jul 12 '12 at 8:09











        • That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

          – gaganbm
          Jul 12 '12 at 8:14











        • okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:16
















        1














        Use the class BigInteger. ( I am not sure if that will even work for such huge integers )






        share|improve this answer
























        • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01











        • @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

          – gaganbm
          Jul 12 '12 at 8:06













        • yep...so can u explain me this in a bit more detail.

          – Himanshu Aggarwal
          Jul 12 '12 at 8:09











        • That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

          – gaganbm
          Jul 12 '12 at 8:14











        • okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:16














        1












        1








        1







        Use the class BigInteger. ( I am not sure if that will even work for such huge integers )






        share|improve this answer













        Use the class BigInteger. ( I am not sure if that will even work for such huge integers )







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:31









        gaganbmgaganbm

        1,28211027




        1,28211027













        • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01











        • @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

          – gaganbm
          Jul 12 '12 at 8:06













        • yep...so can u explain me this in a bit more detail.

          – Himanshu Aggarwal
          Jul 12 '12 at 8:09











        • That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

          – gaganbm
          Jul 12 '12 at 8:14











        • okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:16



















        • yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01











        • @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

          – gaganbm
          Jul 12 '12 at 8:06













        • yep...so can u explain me this in a bit more detail.

          – Himanshu Aggarwal
          Jul 12 '12 at 8:09











        • That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

          – gaganbm
          Jul 12 '12 at 8:14











        • okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

          – Himanshu Aggarwal
          Jul 12 '12 at 8:16

















        yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:01





        yeah i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:01













        @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

        – gaganbm
        Jul 12 '12 at 8:06







        @HimanshuAggarwal You can try a nasty thing like writing your own multiply routine for integers. For example, you can have two big integers to represent one HUGE integer. Well, it will be complicated and cumbersome. e.g. if my integers are of size 4 bits, to represent a number like 18, I can use 2 integers : 1 and 8 with appropriate weightages. (1 with weightage 10^1 and 8 with 10^1). So if you can write a code to represent big integers in chunks of small ones with attached weightages, and write your own multiply routine, this can be done.

        – gaganbm
        Jul 12 '12 at 8:06















        yep...so can u explain me this in a bit more detail.

        – Himanshu Aggarwal
        Jul 12 '12 at 8:09





        yep...so can u explain me this in a bit more detail.

        – Himanshu Aggarwal
        Jul 12 '12 at 8:09













        That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

        – gaganbm
        Jul 12 '12 at 8:14





        That was 10^0 for 8, in the previous comment. For multiplication, you have to take care of all the weightages and so on. It will be slow. Very slow. And the concept is loosely based on WallaceTree.

        – gaganbm
        Jul 12 '12 at 8:14













        okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

        – Himanshu Aggarwal
        Jul 12 '12 at 8:16





        okay..... but this is definitely going to be cumbersome..... by the way thanx for this explanation... i'll try to implement this method as well...:-)

        – Himanshu Aggarwal
        Jul 12 '12 at 8:16











        1














        This blog post explains biginteger factorial in java with examples.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

          – Himanshu Aggarwal
          Jul 12 '12 at 7:59
















        1














        This blog post explains biginteger factorial in java with examples.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

          – Himanshu Aggarwal
          Jul 12 '12 at 7:59














        1












        1








        1







        This blog post explains biginteger factorial in java with examples.






        share|improve this answer













        This blog post explains biginteger factorial in java with examples.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:36









        nurettinnurettin

        7,71144464




        7,71144464













        • yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

          – Himanshu Aggarwal
          Jul 12 '12 at 7:59



















        • yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

          – Himanshu Aggarwal
          Jul 12 '12 at 7:59

















        yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

        – Himanshu Aggarwal
        Jul 12 '12 at 7:59





        yes i have used biginteger as well but the compiler fails to display the answer for such a number.....

        – Himanshu Aggarwal
        Jul 12 '12 at 7:59











        0














        Infinity is a special reserved value in the Double class used when you have exceed the maximum number the a double can hold.



        If you want your code to work, use the BigDecimal class, but given the input number, don't expect your program to finish execution any time soon.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01
















        0














        Infinity is a special reserved value in the Double class used when you have exceed the maximum number the a double can hold.



        If you want your code to work, use the BigDecimal class, but given the input number, don't expect your program to finish execution any time soon.






        share|improve this answer
























        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01














        0












        0








        0







        Infinity is a special reserved value in the Double class used when you have exceed the maximum number the a double can hold.



        If you want your code to work, use the BigDecimal class, but given the input number, don't expect your program to finish execution any time soon.






        share|improve this answer













        Infinity is a special reserved value in the Double class used when you have exceed the maximum number the a double can hold.



        If you want your code to work, use the BigDecimal class, but given the input number, don't expect your program to finish execution any time soon.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '12 at 7:35









        BohemianBohemian

        299k65428567




        299k65428567













        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01



















        • yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

          – Himanshu Aggarwal
          Jul 12 '12 at 8:01

















        yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:01





        yes i have used biginteger as well but the compiler fails to display the answer for such a number so is there any other way out???

        – Himanshu Aggarwal
        Jul 12 '12 at 8:01











        0














        The above solutions for your problem (8785856!) using BigInteger would take literally hours of CPU time if not days. Do you need the exact result or would an approximation suffice?



        There is a mathematical approach called "Sterling's Approximation
        " which can be computed simply and fast, and the following is Gosper's improvement:
        enter image description here






        share|improve this answer




























          0














          The above solutions for your problem (8785856!) using BigInteger would take literally hours of CPU time if not days. Do you need the exact result or would an approximation suffice?



          There is a mathematical approach called "Sterling's Approximation
          " which can be computed simply and fast, and the following is Gosper's improvement:
          enter image description here






          share|improve this answer


























            0












            0








            0







            The above solutions for your problem (8785856!) using BigInteger would take literally hours of CPU time if not days. Do you need the exact result or would an approximation suffice?



            There is a mathematical approach called "Sterling's Approximation
            " which can be computed simply and fast, and the following is Gosper's improvement:
            enter image description here






            share|improve this answer













            The above solutions for your problem (8785856!) using BigInteger would take literally hours of CPU time if not days. Do you need the exact result or would an approximation suffice?



            There is a mathematical approach called "Sterling's Approximation
            " which can be computed simply and fast, and the following is Gosper's improvement:
            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 31 '15 at 12:18









            JoolJool

            1,349109




            1,349109























                0














                 import java.util.*;
                import java.math.*;

                class main
                {
                public static void main(String args)
                {
                Scanner sc= new Scanner(System.in);

                int i;
                int n=sc.nextInt();


                BigInteger fact = BigInteger.valueOf(1);

                for ( i = 1; i <= n; i++)
                {
                fact = fact.multiply(BigInteger.valueOf(i));
                }
                System.out.println(fact);

                }
                }





                share|improve this answer
























                • I think the answer would be better without the scanner class.

                  – MaxPower
                  Aug 7 '17 at 19:21











                • where you declare or defined multiply????

                  – Ved Prakash
                  Aug 7 '17 at 19:48
















                0














                 import java.util.*;
                import java.math.*;

                class main
                {
                public static void main(String args)
                {
                Scanner sc= new Scanner(System.in);

                int i;
                int n=sc.nextInt();


                BigInteger fact = BigInteger.valueOf(1);

                for ( i = 1; i <= n; i++)
                {
                fact = fact.multiply(BigInteger.valueOf(i));
                }
                System.out.println(fact);

                }
                }





                share|improve this answer
























                • I think the answer would be better without the scanner class.

                  – MaxPower
                  Aug 7 '17 at 19:21











                • where you declare or defined multiply????

                  – Ved Prakash
                  Aug 7 '17 at 19:48














                0












                0








                0







                 import java.util.*;
                import java.math.*;

                class main
                {
                public static void main(String args)
                {
                Scanner sc= new Scanner(System.in);

                int i;
                int n=sc.nextInt();


                BigInteger fact = BigInteger.valueOf(1);

                for ( i = 1; i <= n; i++)
                {
                fact = fact.multiply(BigInteger.valueOf(i));
                }
                System.out.println(fact);

                }
                }





                share|improve this answer













                 import java.util.*;
                import java.math.*;

                class main
                {
                public static void main(String args)
                {
                Scanner sc= new Scanner(System.in);

                int i;
                int n=sc.nextInt();


                BigInteger fact = BigInteger.valueOf(1);

                for ( i = 1; i <= n; i++)
                {
                fact = fact.multiply(BigInteger.valueOf(i));
                }
                System.out.println(fact);

                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 7 '17 at 19:17









                pravin deshatwarpravin deshatwar

                1




                1













                • I think the answer would be better without the scanner class.

                  – MaxPower
                  Aug 7 '17 at 19:21











                • where you declare or defined multiply????

                  – Ved Prakash
                  Aug 7 '17 at 19:48



















                • I think the answer would be better without the scanner class.

                  – MaxPower
                  Aug 7 '17 at 19:21











                • where you declare or defined multiply????

                  – Ved Prakash
                  Aug 7 '17 at 19:48

















                I think the answer would be better without the scanner class.

                – MaxPower
                Aug 7 '17 at 19:21





                I think the answer would be better without the scanner class.

                – MaxPower
                Aug 7 '17 at 19:21













                where you declare or defined multiply????

                – Ved Prakash
                Aug 7 '17 at 19:48





                where you declare or defined multiply????

                – Ved Prakash
                Aug 7 '17 at 19:48











                0














                Try this:



                import java.math.BigInteger;

                public class LargeFactorial
                {
                public static void main(String args)
                {
                int n = 50;
                }
                public static BigInteger factorial(int n)
                {
                BigInteger result = BigInteger.ONE;
                for (int i = 1; i <= n; i++)
                result = result.multiply(new BigInteger(i + ""));
                return result;
                }
                }





                share|improve this answer
























                • Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                  – Gautam Sarkar
                  Jun 10 '18 at 3:31
















                0














                Try this:



                import java.math.BigInteger;

                public class LargeFactorial
                {
                public static void main(String args)
                {
                int n = 50;
                }
                public static BigInteger factorial(int n)
                {
                BigInteger result = BigInteger.ONE;
                for (int i = 1; i <= n; i++)
                result = result.multiply(new BigInteger(i + ""));
                return result;
                }
                }





                share|improve this answer
























                • Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                  – Gautam Sarkar
                  Jun 10 '18 at 3:31














                0












                0








                0







                Try this:



                import java.math.BigInteger;

                public class LargeFactorial
                {
                public static void main(String args)
                {
                int n = 50;
                }
                public static BigInteger factorial(int n)
                {
                BigInteger result = BigInteger.ONE;
                for (int i = 1; i <= n; i++)
                result = result.multiply(new BigInteger(i + ""));
                return result;
                }
                }





                share|improve this answer













                Try this:



                import java.math.BigInteger;

                public class LargeFactorial
                {
                public static void main(String args)
                {
                int n = 50;
                }
                public static BigInteger factorial(int n)
                {
                BigInteger result = BigInteger.ONE;
                for (int i = 1; i <= n; i++)
                result = result.multiply(new BigInteger(i + ""));
                return result;
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 10 '18 at 2:50









                Gautam SarkarGautam Sarkar

                266




                266













                • Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                  – Gautam Sarkar
                  Jun 10 '18 at 3:31



















                • Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                  – Gautam Sarkar
                  Jun 10 '18 at 3:31

















                Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                – Gautam Sarkar
                Jun 10 '18 at 3:31





                Sorry sir if my answer is wrong. I tried to answer so that he/she can find factorial of any number. All he/she has to do is change the value of n.

                – Gautam Sarkar
                Jun 10 '18 at 3:31











                0














                    Scanner r = new Scanner(System.in);
                System.out.print("Input Number : ");
                int num = r.nextInt();
                int ans = 1;
                if (num <= 0) {
                ans = 0;
                }
                while (num > 0) {
                System.out.println(num + " x ");
                ans *= num--;
                }
                System.out.println("bb=" + ans);





                share|improve this answer
























                • edit your answer and add short explanation to your code

                  – barbsan
                  Nov 21 '18 at 11:21
















                0














                    Scanner r = new Scanner(System.in);
                System.out.print("Input Number : ");
                int num = r.nextInt();
                int ans = 1;
                if (num <= 0) {
                ans = 0;
                }
                while (num > 0) {
                System.out.println(num + " x ");
                ans *= num--;
                }
                System.out.println("bb=" + ans);





                share|improve this answer
























                • edit your answer and add short explanation to your code

                  – barbsan
                  Nov 21 '18 at 11:21














                0












                0








                0







                    Scanner r = new Scanner(System.in);
                System.out.print("Input Number : ");
                int num = r.nextInt();
                int ans = 1;
                if (num <= 0) {
                ans = 0;
                }
                while (num > 0) {
                System.out.println(num + " x ");
                ans *= num--;
                }
                System.out.println("bb=" + ans);





                share|improve this answer













                    Scanner r = new Scanner(System.in);
                System.out.print("Input Number : ");
                int num = r.nextInt();
                int ans = 1;
                if (num <= 0) {
                ans = 0;
                }
                while (num > 0) {
                System.out.println(num + " x ");
                ans *= num--;
                }
                System.out.println("bb=" + ans);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 11:00









                Roshan TharangaRoshan Tharanga

                11




                11













                • edit your answer and add short explanation to your code

                  – barbsan
                  Nov 21 '18 at 11:21



















                • edit your answer and add short explanation to your code

                  – barbsan
                  Nov 21 '18 at 11:21

















                edit your answer and add short explanation to your code

                – barbsan
                Nov 21 '18 at 11:21





                edit your answer and add short explanation to your code

                – barbsan
                Nov 21 '18 at 11:21











                -1














                To really find out the factorial of this number you should use PYTHON functions , and try to open the task manager and see how much memory the compiler takes .
                After that you will know that how much time JVM is going to take ,as PYTHON is the best language for numerical calculations.






                share|improve this answer




























                  -1














                  To really find out the factorial of this number you should use PYTHON functions , and try to open the task manager and see how much memory the compiler takes .
                  After that you will know that how much time JVM is going to take ,as PYTHON is the best language for numerical calculations.






                  share|improve this answer


























                    -1












                    -1








                    -1







                    To really find out the factorial of this number you should use PYTHON functions , and try to open the task manager and see how much memory the compiler takes .
                    After that you will know that how much time JVM is going to take ,as PYTHON is the best language for numerical calculations.






                    share|improve this answer













                    To really find out the factorial of this number you should use PYTHON functions , and try to open the task manager and see how much memory the compiler takes .
                    After that you will know that how much time JVM is going to take ,as PYTHON is the best language for numerical calculations.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 22 '16 at 22:12









                    akshay katheriaakshay katheria

                    194




                    194























                        -1














                        import java.util.Scanner;


                        public class factorial {
                        public static void main(String args) {
                        System.out.println("Enter the number : ");
                        Scanner s=new Scanner(System.in);
                        int n=s.nextInt();
                        factorial f=new factorial();
                        int result=f.fact(n);
                        System.out.println("factorial of "+n+" is "+result);
                        }
                        int fact(int a)
                        {
                        if(a==1)
                        return 1;
                        else
                        return a*fact(a-1);
                        }

                        }





                        share|improve this answer




























                          -1














                          import java.util.Scanner;


                          public class factorial {
                          public static void main(String args) {
                          System.out.println("Enter the number : ");
                          Scanner s=new Scanner(System.in);
                          int n=s.nextInt();
                          factorial f=new factorial();
                          int result=f.fact(n);
                          System.out.println("factorial of "+n+" is "+result);
                          }
                          int fact(int a)
                          {
                          if(a==1)
                          return 1;
                          else
                          return a*fact(a-1);
                          }

                          }





                          share|improve this answer


























                            -1












                            -1








                            -1







                            import java.util.Scanner;


                            public class factorial {
                            public static void main(String args) {
                            System.out.println("Enter the number : ");
                            Scanner s=new Scanner(System.in);
                            int n=s.nextInt();
                            factorial f=new factorial();
                            int result=f.fact(n);
                            System.out.println("factorial of "+n+" is "+result);
                            }
                            int fact(int a)
                            {
                            if(a==1)
                            return 1;
                            else
                            return a*fact(a-1);
                            }

                            }





                            share|improve this answer













                            import java.util.Scanner;


                            public class factorial {
                            public static void main(String args) {
                            System.out.println("Enter the number : ");
                            Scanner s=new Scanner(System.in);
                            int n=s.nextInt();
                            factorial f=new factorial();
                            int result=f.fact(n);
                            System.out.println("factorial of "+n+" is "+result);
                            }
                            int fact(int a)
                            {
                            if(a==1)
                            return 1;
                            else
                            return a*fact(a-1);
                            }

                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 1 '18 at 20:20









                            Ruchica SinghRuchica Singh

                            1




                            1






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11446973%2ffind-factorial-of-large-numbers-in-java%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)