Solved

How to compute large factorial values

Posted on 2015-02-21
8
76 Views
Last Modified: 2015-03-01
I am trying to compute a value for 80! (80 factorial) in Java. I have tried both long and BigInteger types without success. I have seen where Guava has a BigIntegerMath class but, unfortunately, I am not allowed to load those jar files. Is there any I can accomplish this without the BigIntegerMath class? If so, can an example be provided?
0
Comment
Question by:awking00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40623145
Without success is not an information we can work with. Especially as factorials with BigInteger works.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 40623333
I think "Without success" is another word for "Overflow". 80! is a fairly big number and would bust the long type easily.

You could try with double (and loose precision).

I'fe googled a bit and found out that the Java platform (SE7) supports BigInteger ... so it should be usable to you w/o any external jar (see here for info about that class).

Maybe the class has no overloaded opertors, so you have to use methods to do calculations - i.e. (syntax not checked, more concept ...)

BigInteger a;
BigInteger b;
BigInteger product;

a = someValue;
b = anotherValue;
product = a.multiply(b);

Open in new window

0
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 40623803
I'm not sure what problem you're having with BigInteger.  It should be exactly what you need.
Something along these lines:

		BigInteger fac = new BigInteger("1") ;

		for (int i = 2 ; i <= 80 ; i++) {
			BigInteger multiplier = new BigInteger(Integer.toString(i)) ;
			fac = fac.multiply(multiplier) ;
		}

		System.out.println("Factorial 80 is " + fac) ;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 40625061
Just wanted to point out an alternative to Doug's solution (don't award any points to my comment), it wouldn't be much different in terms of efficiency but just feels better to not convert to strings...
		BigInteger fac = BigInteger.ONE;

		for (int i = 2 ; i <= 80 ; i++) {
			BigInteger multiplier = BigInteger.valueOf(i);
			fac = fac.multiply(multiplier);
		}

		System.out.println("Factorial 80 is " + fac);

Open in new window

0
 
LVL 27

Expert Comment

by:dpearson
ID: 40625101
Oh yeah - mccarl's approach is cleaner.

I'm just used to only using BigInteger when I have a value that needs to be represented as a String so I'm always using the String constructor - but of course that's not necessary here :)

Doug
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40638634
Sorry Doug,

My intention was not to steal points off you, I even asked for no points! :)
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40638732
Haha - no problem mccarl.

Personally I'm none too concerned about the points on EE - I think the goal of just helping people get good advice should be enough and you made this advice better :)

Doug
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 46
CSV file parsing thru Java 13 54
servlet and mdb, jms error 1 83
spring maven example issues 3 109
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question