Computer Science Canada

Factorial Function Unlimited

Author:  randint [ Mon Oct 29, 2012 3:55 pm ]
Post subject:  Factorial Function Unlimited

This factorial function is identical to what I built previously except that the input is limited by only the RAM avaliable to the Java Virtual Machine, it uses arbitrary-precision integers to calculate, it is super slow but it works!

Author:  mirhagk [ Tue Oct 30, 2012 9:58 am ]
Post subject:  RE:Factorial Function Unlimited

this is one of the nice parts about haskell. In haskell you can simply:

factorial 0 = 0
factorial n = n*factorial(n-1)

Haskell by default will decide what type to use, and one of it's types is an abitrarily large integer.


The use of BigInteger IMO is completely underused. ANY calculator app should use BigInteger as speed and memory usage aren't as important as accuracy.

Author:  TerranceN [ Tue Oct 30, 2012 11:46 am ]
Post subject:  RE:Factorial Function Unlimited

Using lazy evaluation you can even create the infinite list of ALL factorial values:

Haskell:

factorial = 1:zipWith (*) factorial [1..]

-- You can now do
factorial !! 5
-- and get 120

Author:  mirhagk [ Tue Oct 30, 2012 11:54 am ]
Post subject:  RE:Factorial Function Unlimited

@OP if you think your program is neat, I would HIGHLY suggest you look into haskell. It's fun.
http://learnyouahaskell.com/ Great place to learn.

Author:  randint [ Sun Nov 18, 2012 11:38 am ]
Post subject:  Re: Factorial Function Unlimited

It is not like I do not understand recursion--I know that n! = n * (n - 1)! for all n > 0, I do not use it because the computational complexity is terrible. It is easy to code but takes an infinite amount of time, with great possibility of a StackOverflowError when the input is >= 2000 (?).

Author:  randint [ Sun Nov 18, 2012 11:51 am ]
Post subject:  RE:Factorial Function Unlimited

By the way, mirhagk, 0! is 1 by definition.

Author:  md [ Sun Nov 18, 2012 12:48 pm ]
Post subject:  RE:Factorial Function Unlimited

mirhagk, stop hounding people to try Haskell. Post something about it in the general forums and present some reasons for trying it.


: