Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Factorial Function Unlimited
Author Message
randint

Posted: 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.

Filename:  Factorial.java
Filesize:  1.01 KB

mirhagk

Posted: Tue Oct 30, 2012 9:58 am   Post subject: RE:Factorial Function Unlimited

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

Posted: 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

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

Posted: 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

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

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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 7 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: