OK, so I'm working on a combinations problem (I'm struggling through it, so if anyone is curious, I'm trying to find the total combinations to arrange n people in a circle of k chairs, such that no person is sitting next to anyone else)
Regardless, what I'm trying to figure out right now is how to do operations using large numbers. To be specific, I probably just need to do multiplication and division. Also, I would imagine that the largest number I would ever need is maybe (1000!).
The largest data type I know is long which is 64 bits. However, that's certainly not big enough, and I'm not sure how to get around this. One option I've used in the past is to create a linked list (one node for each 3 digits). However, I just don't like that solution - epsecially since I will need to think up an algorithm to multiply and divide with it.
Do you need an exact answer? If not then type double or long double might work.
Also, when doing combinatorial calculations, it's easy to screw up and compute a really large numerator, then a really large denominator and then divide. Be smart and alternate multiplication and division to avoid the overflow if possible.
You are multiplying 2 byitseft 64 times in 2 ^ 64. But in 1000 factorial, you are multiplying more than 64 numbers, all greater than 2. Hence, you will get a number larger than 2 ^ 64.
Do you need an exact answer? If not then type double or long double might work.
Actually, I don't
I need to print out a number that is a modulo of 10000000 (or something close to that). So I suspect there is some way to do this avoiding large numbers altogether. I just am not sure what it is.
If you want a reference, my problem I'm trying to solve is fairly similar to this
I need to print out a number that is a modulo of 10000000 (or something close to that). So I suspect there is some way to do this avoiding large numbers altogether. I just am not sure what it is.
Indeed there is. Just modulo by the value after every multiplication, since every digit outside that range will not affect the part of the result you're looking for with further multiplication.
Might be of interest as a start on modular arithmetic online references:
http://www.artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction