Factorial.

Aug 23, 2012 at 2:07pm
Hi all,

I'm studying C++ and I wrote down a simple code to evaluate the factorial of a number using the recursivity property of functions.
I can't understand why it works fine for number smaller than 65 and for bigger ones it returns always 0.

the code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

long factorial (long a)
{
	if (a>1)
	{
		return (a=a*factorial(a-1));
	}
	else
	{
		return (1);
	 }
	 
 }
 
 int main ()
 {
	 long num;
	 cout << "Digita il numero di cui vuoi calcolare il fattoriale: ";
	 
	 cin >> num;
	 
	 cout << num << "! = " << factorial (num) << "\n";
	 
	 return 0;
 }
Aug 23, 2012 at 2:10pm
Do you mean passing a number greater than 65? Cause those are some massive numbers, you shouldn't even really get to 20
Aug 23, 2012 at 2:17pm
Yes, I mean passing...
What should I do if I would need to evaluated the factorial of a big number?
Aug 23, 2012 at 2:45pm
Well, 65! is a 91 digit number according to wolframalpha. I think there's a library out there called bignum that handles numbers like this.
Aug 23, 2012 at 2:47pm
Isn't it much more efficient to write a factorial program with a loop, instead of recursively. Like, I can imagine that for extremely large numbers, the program will begin to work very slowly.
Aug 23, 2012 at 2:56pm
Yes, you are absolutely right noysoffer, I wrote it as exercise to use the recursivity property. It is not supposed to be optimized.

ResidentBiscuit, thanks, I'll look for this library.
Aug 23, 2012 at 3:59pm
@OP(original poster):
I can't understand why it works fine for number smaller than 65 and for bigger ones it returns always 0.


If that's what you say, then use long double instead of long int(long)
Last edited on Aug 23, 2012 at 4:00pm
Aug 23, 2012 at 4:05pm
Here
http://cpp.forum24.ru/?1-3-0-00000049-000-0-0-1344549578

is some interesting methods of calculating the factorial. And here

http://cpp.forum24.ru/?1-3-0-00000047-000-0-0-1344354047

are ranges of values of the factorial for some fundamental types.

Though the text is written in Russian I think you can translate it with the google.
Aug 24, 2012 at 12:41am
If that's what you say, then use long double instead of long int(long)


This won't help at all. He doesn't need floating point at all. A lot of the space used in doubles is reserved for the mantissa.
Aug 24, 2012 at 4:13am
Here's the problem. OP just assumed that his program was returning correct results (when for obvious reasons, it wasn't). 21! overflows 64-bit integers, but if you're not checking your results you'll only see a list of large numbers. For example, 30! modulo 2^64 is 9682165104862298112, but 30! is 265252859812191058636308480000000.
But, 66! just happens to be divisible by 2^64, so no matter what, at this point you'd find that your integer turns up zero.

Here's a fun idea: try proving that ∀i∈N0 (2^i+2)! is divisible by 2^2^i.
Aug 24, 2012 at 11:40am
helios,

Your post is quite intersting to me as I'm going to try to understand if the code is correct or not.
You said that for obvious reasons it is wrong, but they are not obvious at all to me, could you please explain me better the problem?

Many thanks in advance
Aug 24, 2012 at 2:14pm
Isn't it much more efficient to write a factorial program with a loop, instead of recursively. Like, I can imagine that for extremely large numbers, the program will begin to work very slowly.
No, it is not. The complexity is linear in both cases.

Resident Biscuit wrote:
(using long double) won't help at all. He doesn't need floating point at all. A lot of the space used in doubles is reserved for the mantissa.
¿what's your point?
All that matters is that you can represent bigger numbers with long double

@OP: your code is fine. You run out of fingers to count, that's all
Aug 24, 2012 at 2:28pm
@ne555: I was afraid there was other problems the the overflow thing I wasn't able to spot.
Aug 24, 2012 at 3:14pm
mentulapert wrote:
You said that for obvious reasons it is wrong, but they are not obvious at all to me, could you please explain me better the problem?
It's explained in the sentence immediately after that.

ne555 wrote:
The complexity is linear in both cases.
I want to say that the space complexity of the iterative version is constant while the space complexity of the recursive version is linear, but the cost of multiplying rises much faster than the cost of recursing.
Topic archived. No new replies allowed.