• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

factorial

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to compute the factorial of a set of large numbers. What is the best way to do this? Bearing in mind I need it done as quickly as possible. Should I use the gamma function and if so how do I accomplish this in Java.
Thanks
James
[ October 28, 2002: Message edited by: James Kerruish ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really needed to do it "as quickly as possible", you should probably program it in assembler, not in Java. But I guess you didn't really mean that, so, what are your actual performance constraints? Are you sure that a simple algorithm wouldn't suffice???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A gamma function? I seriously doubt it's useful here - for integer values, it's much simpler to use a straightforward factorial implementation. If that's not good enough, for your situation (calculating factorials for many numbers) it might well be worthwhile to pre-generate a table, something like this:

Of course, you can adjust MAX_INPUT as desired. And I believe BigInteger used to be annoyingly slow, but has since been improved. You may wish to replace BigInteger with a custom class if you're using an older JDK version. And of course, if you're not interested in the factorial of any number larger than 20, you can just use a long rather than BigInteger.
[ October 28, 2002: Message edited by: Jim Yingst ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic