Well I know that itoa and sprintf are there for this task, but all these require a pre-defined size of int (like int , long int, long long int), but what if I need to add or subtract something from factorial of 99, which is let's say stored in a char string s[200]; (and that is beyond the limits of even long long int).....what should I do???
Asked
Active
Viewed 94 times
0
-
Why do you want to do this? – zoska Mar 11 '14 at 13:55
-
let's say I want to add 10 or 20 to the factorial of 99, then what should I do?? – 6565850 Mar 11 '14 at 13:56
-
Then you are going to implement that yourself or use a library that handles large numbers. – P.P Mar 11 '14 at 13:57
-
Use a library that can handle arbitrarily long numbers. – Bart Friederichs Mar 11 '14 at 13:58
-
@BlueMoon how to implement it, and most importantly how to store it?? – 6565850 Mar 11 '14 at 13:59
-
@BartFriederichs which library can I use?? – 6565850 Mar 11 '14 at 14:00
-
@BlueMoon what's Big integer?? – 6565850 Mar 11 '14 at 14:00
-
@6565850 Look at [GMP library](https://gmplib.org/) and its code. – P.P Mar 11 '14 at 14:01
-
https://www.openssl.org/docs/crypto/bn_internal.html#The_BIGNUM_structure – Abhineet Mar 11 '14 at 14:03
-
@BlueMoon could you please send me a link??? – 6565850 Mar 11 '14 at 14:04
-
@Abhineet that link is beyond my understanding??? – 6565850 Mar 11 '14 at 14:10
-
@BlueMoon isn't there any other method (like structure or pointer formation) rather than using external library.... – 6565850 Mar 11 '14 at 14:15
-
The question is "Why do you need an integer whose range is larger than unsigned long long?" Think about the ways in which you can work around your problem without converting them (and most probably, there must be a way). If you do have to use them as int, then you have to use some library." – Abhineet Mar 11 '14 at 14:51
-
http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial – Abhineet Mar 11 '14 at 14:58
-
http://stackoverflow.com/questions/1384160/calculating-factorial-of-large-numbers-in-c – Abhineet Mar 11 '14 at 14:59
1 Answers
0
one of the way to handle such large numbers is to use integer array. such as
int a[200]
and perform operations on the array as you perform operations on numbers using basic arithmetic of considering each digit of a number i.e for example consider multiplying 2 numbers 1234*4567 what you do is, store 1234 in a[] and 4567 in b[]. use a temporary array c[] then,
4*7=28 so next
3*7+2(which can be computed as (int)28/10)=23
2*7+2=16
1*7+1=8
so you get 8638 in first iteration store it in c[] then, do the same using 6 i.e 1234*6 and do 8638+1234*6*10 i think now you got this basic maths. use this type basic maths method to perform operations on large numbers stored in array. all operation can be implemented by using such digit level operations.

LearningC
- 3,182
- 1
- 12
- 19
-
but how would I add even the integer array??? suppose last three digits are 4, 6, 7 how would I do normal addition??? – 6565850 Mar 12 '14 at 14:46
-
@6565850 i have explained how to multiply large numbers. addition is also same add the numbers digit wise. – LearningC Mar 12 '14 at 15:52