With the recent Python bashing, here's an attempt to show Python's strengths.
Your challenge is to write a program that calculates the factorial of as high a number n
as possible within 10 seconds.
Your score will be (highest n for your program on your machine)/(highest n for my program on your machine)
Rules
- You must calculate an exact integer solution. Since the factorial would be much higher than what can fit in a 64 bit unsigned integer, you can use strings if your language does not support large integers
- Standard loopholes are forbidden. Particularly, you cannot use any external resources.
- Only the calculation part(this includes time for any workarounds using strings) adds to the total time which should be under 10 seconds on average.
- Single threaded programs only.
- You must store the output in an easily printable form (as printing takes time) (see my program below), string, variable, character array, etc.
EDIT:
- Your program must give the correct output for all
n
:1 <= n <= (your highest n)
EDIT2:
- I hate to say this explicitly but using your language's built-in factorial functions falls under the standard loopholes http://meta.codegolf.stackexchange.com/a/1078/8766 Sorry Mathematica and Sage
My program
from __future__ import print_function
import time
def factorial( n ):
return reduce( ( lambda x , y : x * y ) , xrange( 1 , n + 1 ) , 1 )
start = time.clock()
answer = factorial( 90000 )
end = time.clock()
print ( answer )
print ( "Time:" , end - start , "sec" )
Highest score wins.
For the record, my code can manage n = 90000
in about 9.89
seconds on a Pentium 4 3.0 GHz
EDIT: Can everyone please add the score rather than just the highest n. Just the highest n
has no meaning by itself as it depends on your hardware. It's impossible to have an objective winning criterion otherwise. ali0sha's anwer does this correctly.
We have a winner. I didn't accept the java answer https://codegolf.stackexchange.com/a/26974/8766 as it kind of skirts close to http://meta.codegolf.stackexchange.com/a/1080/8766
operator.mul
instead of the lambda function \$\endgroup\$factorial(Inf)
, returnsInf
in a fraction of a second. \$\endgroup\$