Sir plz tell why my test case are failed
Hello @rakesh800650,
The logic you have applied would not work.
Reason:
100! is a very very large number and there is no datatype available to store it.
Solution:
You matrix to store the result of the multiplication.
Approach:
factorial()
- Create an array ‘res[]’ of MAX size where MAX is number of maximum digits in output.
- Initialize value stored in ‘res[]’ as 1 and initialize ‘res_size’ (size of ‘res[]’) as 1.
- Do following for all numbers from x = 2 to n.
… Multiply x with res[] and update res[] and res_size to store the multiplication result. - Print res[] from right to left.
easy?
Algorithm for multiplication:
multiply(res[], x)
- Initialize carry as 0.
- Do following for i = 0 to res_size – 1
….a) Find value of res[i] * x + carry. Let this value be prod.
….b) Update res[i] by storing last digit of prod in it.
….c) Update carry by storing remaining digits in carry. - Put all digits of carry in res[] and increase res_size by the number of digits in carry.
So, let’s understand it with the help of an example:
Relate the approach with the way you perform multiplication in mathematics.
Why are we using a matrix?
The factorial results in a value greater than the range of long long.
Example:
Input :50
Output : 30414093201713378043612608166064768844377641568960512000000000000
Let’s understand the logic for multiplication using a small example:
76*4
-
In mathematics:
76
x4
__
304
After multiplying 4 with 6 the result is 24
adding carry to it i.e. 0 initially. So, the result is 24 only.
So, write the last digit as it is i.e. 4
Then, take 2 as carry over 7.
Now multiplying 4 with 7, the result is 28
Add carry to it 28+2=30
Write the last digit as it is i.e. 0
And take 3 as carry.
As there are no more digits left to multiply with 4.
So, write carry as it is in the answer i.e. 3.
Hence, the answer is 304
Now, the same approach has been followed in the code.
The only difference is. The result here is stored from right to left.
In the case of the array, it will be stored from left to right
-
With array:
Create an array of size 1000: int res[10000];
Initially res[] stores 76 as res[0]=6 and res[1]=7 (the digit at one’s place is at index 0)
add res_size=2;
//initialize carry to zero
carry=0;
Now multiply res[] with 4:
for i=0 to res_size-1:
2.1. i=0:
prod=res[i] 4+carry=6 4+0=24
res[i]=res%10=4 (gives the digit at one’s place)
carry=res/10=2 (rest goes to carry)
2.2. i=1:
prod=res[i] 4+carry=7 4+2=30
res[i]=res%10=0 (gives the digit at one’s place)
carry=res/10=3 (rest goes to carry)
End loop
\insert carry to the res[]
while(carry>0)
res[res_size++]=c%10=3;
c=c/10=0;
end loop
This is how the multiplication is happening in the logic that i have specified in my previous reply.
Note:
As result in array is stored from left to right in res[].
So, print res from right to left i.e. from index res_size-1 to 0.
Hope, this would help.
Give a like if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.