I need help about how to make a factorail progarm in c++ .
factorail program in c++
Collapse
X
-
What is the maximum input? I think 12! > INT_MAX. You can use the recurrence relation fac(n) = n*fac(n-1), however it's really sluggish. You could convert it to a loop, since it's tail recursion, however it's still sluggish for big numbers.
If there exists a way to quickly calculate n! for large n, then factoring becomes trivial. Suppose a < b. Then gcd(a*b,floor(s qrt(a*b))!) = a, always. Factoring is the obstacle that keeps public key encryption systems safe.
In combinatorics, they need really huge factorial numbers to count things. In practice, it's acceptable to approximate them. Stirling's approximation may be used, which is usually 1% accurate (first two digits are correct). There is also Lanczos's approximation, which is an infinite series which can repeatedly be added for arbitrary precision. -
/****Program to get the factorial****** *************** **/
#include <iostream>
using namespace std;
int main()
{
int num, fact;
cout << "Enter a positive and I will give you the factorial: ";
cin >> num;
cout << "\nThe factorial of " << num << " is: ";
fact = 1;
if (num > 0)
while (num > 0)
{
fact = fact*num;
num--;
}
cout << fact << "\n";
system ("pause");
}Comment
Comment