Does anyone know how to multiply a number that is unlimited in length to another unlimited number? it cannot be an integer because the integer cannot contain unlimited number of variables! Can anyone help me out with a source code for this?
This is a discussion on Source Code for multiplication... help! within the C Programming forums, part of the General Programming Boards category; Does anyone know how to multiply a number that is unlimited in length to another unlimited number? it cannot be ...
Does anyone know how to multiply a number that is unlimited in length to another unlimited number? it cannot be an integer because the integer cannot contain unlimited number of variables! Can anyone help me out with a source code for this?
What you should do is create a datastructure by yourself which contains the 'unlimited' number and define operations on it.
You could use perhaps a string, struct or array to store the large number.
There are some more threads on this subject on this board. I suggest you take a look at them.
Thatīs a difficult mathematical problem!
Why reinventing the wheel when it is existing yet?
gmp is a good library which will solve your problem. The only limit for the numbersīvalue is your PCīs memory size.
klausi
When I close my eyes nobody can see me...
>Thatīs a difficult mathematical problem!
It is not very difficult. You can just use the techniques you've learned on elementary school.
It is getting harder when doing more advanced things than just the basic operations.Code:5 5 2 1 -------- x 5 5 1 1 0 0 -------- + 1 1 5 5
I figure there's two ways to go about this...
1. Just do the math using the strings, base 10, like you learned in school. This is pretty easy to wrap your mind around.
2. Convert the number you need to multiply into an array of shorts. So we could represent the numbers as say...Where each letter is basically a short you can think of that as a number, base short. The result of this multiplication will create 5 longs....Code:ABCD x EFG -----b is the max value of the unsigned short, and isn't really included in the value we store in memory. Generating these numbers isn't really that tough, but adding them up is a bit of a trick. You'll have to add them as shorts and handle the carries manually.Code:DG (p0) + (CG + DF) * b (p1) + (BG + CF + DE) * b^2 (p2) + (AG + BF + CE) * b^3 (p3) + (AF + BE) * b^4 (p4) + AE * b ^ 5 (p5)
The difficulty in multiplying large numbers is mainly being able to add large numbers.
Callou collei we'll code the way
Of prime numbers and pings!