The factor method does not work well!

Hi this is a factor method, but it prints 0 in the console, please help me thanks

public class Demo { public static void main(String[] args) { Demo obj = new Demo(); System.out.println(obj.factorial(500)); } public int factorial(int n) { int fact = 1; for (int i = 2; i <= n; i++) { fact= fact*i; } return fact; } 

EDITED: will bring back Infinity!

 public class Demo { public static void main(String[] args) { Demo obj = new Demo(); System.out.println(obj.factorial(500)); } public double factorial(long n) { double fact = 1; for (int i = 2; i <= n; i++) { fact= fact*i; } return fact; } } 
+6
java math factorial
source share
6 answers

Since 500! equally 1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 , you cannot put it in an int (range up to 2147483647 ).

  • Using int , you can only save 12! .
  • Using long , you get up to 20!
  • Using double , you get up to 170! .

Here is a solution using BigInteger :

 public static BigInteger factorial(int i) { BigInteger n = BigInteger.valueOf(i); while (--i > 0) n = n.multiply(BigInteger.valueOf(i)); return n; } 
+18
source share

Unable to install 500! on a 32-bit int .

For calculations with large numbers, consider using double or BigInteger , depending on whether you want an approximate or accurate answer.

(In fact, for 500! Even a double will not be enough: Double.MAX_VALUE is 1.7976931348623157E + 308, which "only" will allow you to go to 170! )

+4
source share

There are two things you should learn if you need to calculate a factorial function:

1) Memoization . This will greatly speed up your calculations, since a factor function has a recursive definition. What you do is the previous calculations in the cache, so when you request k! , you can get it in one step by calculating k*((k-1)!) if you have (k-1)! cached.

2) Stirling shift . If you need to calculate large factorials, you can quickly approximate them in this way and with guaranteed error limits so that you can determine if the approximation is acceptable for your application.

If you do nothing, you will find that there is a relatively small k for which you simply cannot calculate k! in a reasonable amount of time.

+2
source share

Grodriguez is right - it is almost certainly caused by integer overflow.

If you test your method with more modest inputs, it will return the right output:

 public static void main(String[] args) { Demo obj = new Demo(); for (int i = 0; i < 10; i++) System.out.println(i + "! = " + obj.factorial(i)); } 

500! massive ; when testing your function, starting with less input will be reasonable.

0
source share

500! way too big to fit long or double.
You will have to use other methods for this.

But first, what program needs 500! ?

0
source share

There is a very good optimization for implementing factorization: see, for example, luschny.de for a good implementation of them in Java. Some require more mathematical insight, and then others ... Good luck in the library :-)

0
source share

All Articles