Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Factorial Function Unlimited
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: 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!


Factorial.java
 Description:
Factorial program - extremely inefficient, but functional.

Download
 Filename:  Factorial.java
 Filesize:  1.01 KB
 Downloaded:  208 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: 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.
TerranceN




PostPosted: 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
mirhagk




PostPosted: 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.
randint




PostPosted: 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 (?).
randint




PostPosted: Sun Nov 18, 2012 11:51 am   Post subject: RE:Factorial Function Unlimited

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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: