FCTRL2
We need to print factorials of numbers up-to 100.
I am successful to do that with doing using an array to represent the digit where each element is a single digit.
But, we can improve by storing multiple digits at same element using datatypes like long long and unsigned long long.
So, I tried implementing that
ull digits[160/8+1];
ull size = 0;
void multiply (int n) {
ull index = 0;
ull t = n;
ull carry = 0;
while(carry != 0 || index < size) {
ull x = digits[index] * n + carry;
// cout << index << " " << x << " " << carry << "\n";
digits[index] = (x % (ull)1e8);
carry = x / 1e8;
index ++;
}
size = max(size, index);
}
Here we store 8 digits in single element of array.
But it gives wrong answer with some numbers like
21
OUTPUT:
51090942171709440000
REAL:
51090942171709440000
[21, 22, 38, 41, etc.]
It’s always some problem with zero.
Is my logic correct?