![]() |
Calculating Factorial (Recursively & Iteratively)
Calculating the factorial of a number is a basic excercise while learning to program in C, many of us have done it iteratively, it can also be done recursively. I am posting both iterative and recursive versions below.
Code: C
|
Re: Calculating Factorial (Recursively & Iteratively)
loved your functions they are just readable
|
Re: Calculating Factorial (Recursively & Iteratively)
Thanks for this kind of efoorts.
|
Re: Calculating Factorial (Recursively & Iteratively)
Good work ;) but one small comment :p:
Why having int and unsigned int? Just for coherence, and avoiding compilers work: Code:
/* Recursive Version */Code:
/* Iterative Version */ |
Re: Calculating Factorial (Recursively & Iteratively)
Offtopic comment:
Oleber, I would suggest using the code block rather than manually coloring the code blocks. - http://www.go4expert.com/misc.php?do=bbcode#code |
Re: Calculating Factorial (Recursively & Iteratively)
Everything looks good and nice but what's if you want to calculate " 1000! " or the factorial of greater values ? :) The " unsigned int " type can memorize a value between 0 and +4,294,967,295 but " 1000! " is more much greater than the dimension of " long double " which is the greatest data type in C/C++. I think this is a good question. :) I'm waiting suggestions and ideas.
|
Re: Calculating Factorial (Recursively & Iteratively)
The greatest has also the limitation for large numbers and I think you are with the greatest "long double"
|
Re: Calculating Factorial (Recursively & Iteratively)
Yes, it has, but i'm thinking on an algorithm which not calculates the result,but it generates it into a vector or a list. For example you can get a number with more than 1000 digits as result But if I put every digit of the number into a list I could view it and print it on the screen or into a file. So I think it could be a possible solution for this problem because there is no other data types which could memorize such a number. Opinions ?
|
Re: Calculating Factorial (Recursively & Iteratively)
I made it but I don't really like it cuz` it's slow. It took about 6-7 minutes waiting for the result of " 1000! ".
Code:
#include <iostream.h> |
Re: Calculating Factorial (Recursively & Iteratively)
Goodness gracious! 6-7 mins?? Try to rethink your logic!
|
| All times are GMT +5.5. The time now is 02:13. |