Please help. All I had to do was find factorial of n numbers and print but i Still keep getting wrong answer i.e Red Cross as output!!! I’m a starter so please dont judge
Problem code beginner FCTRL2
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
int main()
{ int t;
int n,x,r;
r=1;
scanf("%d", &t);
vector<int>a(t);
for(int i=0;i<t;i++)
{ r=1;
scanf("%d",&n);
x=n;
while(x>=1)
{
r=r*x;
x--;
}
a[i]=r;
}
for(int j=0;j<t;j++)
printf("%d\n",a[j]);
return 0;
}
There is a memory limit on how much a variable can store. In can store ROUGHLY a little more than 10^9 while long long int can store roughly around 10^18 . (9 x 10^18, You can google it for exact number.)
The numbers after 20! are too large to be stored. Your logic is correct but in implementation, you face a overflow error. You need to know trick of “Handling large numbers in c++”
However, if you submit same code in python, same logic and all, you should get AC.
I believe you have not give enough thought to this problem. It asks you to calculate the factorial of numbers in range [1,100].
Here are few points you need to consider to suggest why you get wrong answer :
- Try writing down first 20 factorials. You should come to the conclusion that factorials grow rapidly fast. How large then you imagine 100! would be? In fact 100! has over 150 decimal digits.
- You should read about memory storage in C/C++. Every datatype supports a specified range and not beyond that. It’s basically a overflow error. Some links : link link
- Now that you know that you can’t store such a large answer ( even with long long, maximum stored result can be of 18~19 digits), you need to approach it in a different way (think about strings!)
If you are still unable to solve, refer to this editorial : editorial
The question asks you to find the factorial of a number N, where 1 <= N <= 100.
The function f(x) = x! increases rapidly as x increases.
For example :-
100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Now, refer to the following link for the ranges of the datatypes in C++.
https://msdn.microsoft.com/en-IN/library/s3f49ktz.aspx
As you can see, the range of int is much less to store a number as big as 100!.
Now, the problem is how to store such big numbers?
-
You can store each digit as an element in an array. There is already a nicely written post about it by @kuruma [https://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial]
3
-
You can use a different language that supports BigIntegers, like Java, Python, etc.
1 Like
Don’t use int
. Use a long long
and even unsigned long long
if need be.
I think that this would be enough for the mentioned problem but if someday you need 64 bit integers, use int64_t
.
Use of these data types will not solve the problem if used in the above naive code as they still cannot store those big numbers.
1 Like
long long already are 64 bit integers. There is no advantage for using int64_t.
3 Likes