Computing.Net > Forums > Programming > Factorial Program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Factorial Program

Reply to Message Icon

Name: SURESH
Date: September 21, 2003 at 22:47:15 Pacific
OS: Win XP
CPU/Ram: Athlon XP 2000+/256 DDR R
Comment:

Hello all,

I am trying to do a factorial program which can able to find factorial for large numbers also. I tried with long integer. I got correct result upto 12. I want to do it atleast for 25. Please suggest me a good code for the same.

Thnx in Advance
SURESH



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: September 21, 2003 at 23:14:30 Pacific
Reply:

Suresh-
You've posted here enough to know that we need more information and the code you've done so far before we'll help you much with homework.

First and foremost, you need to tell us which language you're using. From the windows calculator, 25! is:
15511210043330985984000000
This is quite a bit bigger (840,000 bigger, in fact) than long can usually handle (I'm told it depends on the compiler, but isn't int 32 bit and long 64 bit?) If you're doing this in Java, look into using the BigInteger class, which can do this with arbitrarily large numbers.

You will get slightly better results using an unsigned long integer, but even that won't be big enough to hold 25!.

Best of luck,
-SN


0

Response Number 2
Name: Infinite Recursion
Date: September 21, 2003 at 23:19:31 Pacific
Reply:

You know... this sounds like a tricky professor assignment, I miss college...

At any rate, next try calculating the execution sequence for 2000 Towers of Hanoi discs, I would like to compare answers :)

IR


0

Response Number 3
Name: Chi Happens
Date: September 22, 2003 at 22:03:26 Pacific
Reply:

Factorial algorythm...now that's simple.

IR, I am ashamed you did not even suggest it.

To create a factorial algorythm, you simply need to use a recursion function with the base case of 1.

The code would be something like this:
unsigned long CalcFactorial(unsigned long number)
{
    if(number <= 1)
        return 1;
    else
        return number CalcFactorial(number -1);
}

That's it.

Chi Happens


0

Response Number 4
Name: Chi Happens
Date: September 22, 2003 at 22:05:04 Pacific
Reply:

Um...why did the multiplication symbol not appear?

the last line should read:
return number * CalcFactorial(number -1);

Chi Happens


0

Response Number 5
Name: Chi Happens
Date: September 22, 2003 at 22:11:34 Pacific
Reply:

btw, with the following code, i can get up to 33! using Borland C++ Builder 5:

-------------------
#include <iostream>
#include <conio>
-------------------
using namespace std;

unsigned long CalcFactorial(unsigned long);

unsigned long CalcFactorial(unsigned long number)
{
if(number <= 1)
return 1;
else
return number * CalcFactorial(number -1);
}

int main(int argc, char* argv[])
{
cout << "33! = " << CalcFactorial(33) << endl;
getch();
return 0;
}
-------------------

34, however, returns 0 (zero) because it overflows.

Chi Happens


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Factorial Program

Factorial in Pascal www.computing.net/answers/programming/factorial-in-pascal/5692.html

Reverse Eng. www.computing.net/answers/programming/reverse-eng/5648.html

'Factorial' Numbers in VB6 www.computing.net/answers/programming/factorial-numbers-in-vb6/5520.html