aspose file tools*
The moose likes Beginning Java and the fly likes Factorial of numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Factorial of numbers" Watch "Factorial of numbers" New topic

Factorial of numbers

Ram Prasad

Joined: Feb 02, 2008
Posts: 9
Hi Ranchers,

I am trying to write a function for factorial calculation. When i pass number more than 160 as input and trying to get the factorial, the return is Infinity

I am using a double literal to store the data and input is passed as integer n . I have taken the recursive approach

could someone suggest what data type i can use in order to get the factorial even for bigger numbers or correct me if i need to use other mechanism

My code looks like

Thank You
Henry Wong

Joined: Sep 28, 2004
Posts: 19060

Take a look at the BigDecimal and BigInteger class.

An infinity value for double means that you exceeded the upper range for the type -- except for infinity, that is.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor

Joined: Oct 27, 2005
Posts: 19783

BigInteger is the way to go.
There is also a class called BigDecimal, but you shouldn't use that for whole numbers.

How To Ask Questions How To Answer Questions
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
Also you may take a look at Fast Factorial Functions
Looks like its not so simple after all ... They discourage using "simple" recursing method
I agree. Here's the link:
subject: Factorial of numbers