# Source Code for multiplication... help!

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 ...

1. ## Source Code for multiplication... help!

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?

2. 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.

3. 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

4. >Thatīs a difficult mathematical problem!

It is not very difficult. You can just use the techniques you've learned on elementary school.

Code:
```      5 5
2 1
-------- x
5 5
1 1 0 0
-------- +
1 1 5 5```
It is getting harder when doing more advanced things than just the basic operations.

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...
Code:
``` ABCD
x EFG
-----```
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:
```   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)```
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.

The difficulty in multiplying large numbers is mainly being able to add large numbers.