3 ms·
I found this sufficiently uninformative that I went back to http://www.willamette.edu/~fruehr/haskell/evolution.html http://www.willamette.edu/~fruehr/haskell/e
by btilly 5y ago
I found this sufficiently uninformative that I went back to http://www.willamette.edu/~fruehr/haskell/evolution.html http://www.willamette.edu/~fruehr/haskell/evolution.html.
Incidentally, if your language has big integer arithmetic (like Python does), the following is substantially faster than the usual iterative or recursive approaches:
def multiply_range(n, m):
if m < n:
return 1
elif n == m:
return n
else:
mid = (n+m) // 2
return multiply_range(n, mid) * multiply_range(mid+1, m)
def factorial (n):
return multiply_range(1, n)
Try factorial(100000) to see the performance difference. Why this makes a difference is left as an exercise for the reader. (Note, in some languages, like Ruby, converting to a string representation may be the biggest bottleneck.)