Factorial

Can someone comment out the following segment code?

Also, what does the below paragraphs mean?

Notice how in function factorial we included a call to itself, but only if the argument passed was greater than 1,since otherwise the function would perform an infinite recursive loop in which once it arrived to 0 it would continue multiplying by all the negative numbers (probably provoking a stack overflow error on runtime).

This function has a limitation because of the data type we used in its design (long) for more simplicity. The results given will not be valid for values much greater than 10! or 15!, depending on the system you compile it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // factorial calculator
#include <iostream>
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}


We would be more inclined to help if you would politely use proper indentation - reading the code without it is difficult and annoying.
Last edited on
That paragraph pretty much sums it up. You should google factorials..They are pretty basic math though. All the function does is this

factorial( 10) = 10 * factorial( 9 ) = 9 * factorial( 8 ) = 8 * factorial( 7 )....
which is equal to 10 * 9 * 8 * 7 ....

http://en.wikipedia.org/wiki/Factorial

Also I believe if you go on the tutorials on this site they use factorials as an example for a recursive function ( calling itself from inside the function. )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

  // factorial calculator
#include <iostream>
using namespace std;

long factorial (long a) {
if (a > 1)
 return (a * factorial (a-1));
else
return (1); // What will this line output?
}

int main () {
long number;

 cout << "Please type a number: ";
 cin >> number;

 cout << number << "! = " << factorial (number);
 return 0;
}
that means if n == 1 then it will return 1.
Ex; factorial ( 1 ) == 1
Factorial 3 == 3 * 2 * 1 because
3 * 3 - 2 * ( else == 1 ) that just means it ends the recursion. Instead of recursion with n - 1 it just returns 1 because you don't want to multiply by 0 and the negative numbers or the answer will ALWAYS be 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

  // factorial calculator
#include <iostream>
using namespace std;

long factorial (long a) {
if (a > 1) // Let's say 9, there it is a number greater than 1 
 return (a * factorial (a-1)); // 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1  
else
return (1); // This is skipped. 
 // Should we have entered -1 or 0 for example .. it would print 1 into the console.
}

int main () {
long number; // declare 9 as long

 cout << "Please type a number: ";
 cin >> number; // Here, we insert 9.

 cout << number << "! = " << factorial (number); // 9!= 362880
 return 0;
}



Is the reasoning correct?

Thanks!
Last edited on
No. Factorial( a - 1 ).

Factorial( 10 );

if it is > 1 it will multiply a by a - 1. If a == 1 then it multiplies a by 1. So there yeah line 8 is 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 but line 10 [b]is[b] being called. because with line 8 you are calling factorial( a - 1 ) so when a is 2. that would mean it returns 1 because that is less than 2.

Basically say you have 10. It is like doing
1
2
short total( 1 ) , start( 10 );
while( start > 2 ){ --start; total *= start; }; // you can do > 1 but anything times 1 is that number... 


Recursion is calling the function multiple times.
You are calling the function while the value is greater than 1 and decreasing the value each time and if the value is equal to 1 then you end the recursion and do not call the function again. if you put
1
2
3
4
 long long factorial( long a )
{
    return( a * factorial( a - 1 );
}
It will loop through the function for ever and anyways it would just be 0 but would take eternity to get that answer which actually you never even get because the program would just crash

**edit**actually I don't think the program would crash because after it reaches the negative limit it would turn back into a random positive integer and anything times 0 is always zero therefor would just run forever.



**EDIT2**

Basically if the current value of a is greater than 1 it will keep calling the factorial function with a - 1 as a parameter and if a == 1 then it will not call the factorial function anymore therefore returnning the correct result. The reason you are thinking it does nothing is because it returns 1 and not a but that doesn't matter it is returning 1 to the factorial function being called before it not the very first factorial function that is returning the actual value.

It is returning the value to the call before it.

it would be something like
a = a * b * c * d * e * f * g * h * i * j;
b = a - 1;
c = b - 1;
d = c - 1;
e = d - 1;
f = e - 1;
g = f - 1;
h = g - 1;
i = h - 1;
j = 1;

so a = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1


http://www.cprogramming.com/tutorial/lesson16.html
http://www.cplusplus.com/doc/tutorial/functions2/
http://www.danzig.us/cpp/recursion.html
Last edited on
Topic archived. No new replies allowed.