Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message

Advanced Member

Joined: 25 Mar 2009
Posts: 360

Posted: 13 Feb 2010 12:32:20 pm    Post subject:

Hi! What is the fastest way of find the Factorial of a number in java, and can you explain how it works, or how to implement it? My current method is simply a for loop with multiplication. It is kinda slow. I need to be able to do it faster. I know there are faster ways, but I have no Idea how to implement them.

Back to top
darkstone knight

Advanced Member

Joined: 07 Sep 2008
Posts: 438

Posted: 15 Feb 2010 05:49:49 pm    Post subject:


int factorial(int i) {
 if (i == 1)
  return 1;
return i * factorial( i - 1);

the no resursion method:

int n = 10; // calculates 'result = 10!'
int result = 1;
for( int i = n; i > 1; i)
 result *= i;

as far as i know, there isnt a faster way without look up tables

keep in mind that i know C for 4 days...
Back to top
ceci n'est pas une |

Super Elite (Last Title)

Joined: 04 Nov 2003
Posts: 8328

Posted: 15 Feb 2010 10:57:09 pm    Post subject:

There's not really a way to improve on the easy loop, I think. If all you need is an approximate answer, you might try Stirling's approximation.

Last edited by Guest on 15 Feb 2010 10:57:55 pm; edited 1 time in total
Back to top
Authentic INTJ

Super Elite (Last Title)

Joined: 25 Nov 2004
Posts: 3976

Posted: 16 Feb 2010 04:52:26 am    Post subject:

Computed from Simplify[PowerExpand[Normal[E^Series[Log[Gamma[z]],{z,Infinity,19}]]]], here are some more terms:


After rounding, the integer part is 100% accurate only for up to 27! = 10888869450418352160768000000 (and the next few are off by 1, 11, 169, 2686, 45076, 795170, …). Yet, you can steal an additional six perfect results by knowing ahead of time how many multiples of five there are in each factorial via [attachment=3084:CodeCogsEqn.png] (25n means that a number will end in n zeroes), and then rounding to the nearest appropriate power of ten, rather than one.

Note that the "z-1" appears instead of "z" only because I'm compensating for the fact that this is the gamma function and not the factorial. If you don't know what that means, then just be aware that your answers will be in the form of (z-1)! for an input of z if you decide to take this route.

Last edited by Guest on 16 Feb 2010 05:13:47 am; edited 1 time in total
Back to top
μολών λαβέ

Active Member

Joined: 27 Mar 2005
Posts: 569

Posted: 16 Feb 2010 08:20:34 am    Post subject:

If you're going the Stirling route for factorial/gamma, make sure you use Horner for computing the series inside the exponential. Lanczos is a possible alternative, so long as you choose the appropriate coefficient set.


P.S. http://www.luschny.de/math/factorial/FastF...alFunctions.htm

Last edited by Guest on 16 Feb 2010 08:24:53 am; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours