Thread: Source Code for multiplication... help!

  1. #1
    Destine
    Guest

    Red face 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. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    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...

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >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.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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...
    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 05:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM