Thread: Storing a very long number

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Storing a very long number

    I have a very long number, lets say
    12123423543456546747578934578934578934578975893475 89375893758934573578934753475897689574689897348967 54896758946758567873894573254789723573589235787897 574208527357345897
    An int or a long unsigned int would definitely not be able to store this number. I would like to do some mathematical computation using large numbers, somewhere around 100-300 numbers. Is there any way that i could make mathematical calculation on these large numbers?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There are libraries around that support storage of, and operations on, large integer types. Have a look at the GNU MP Bignum Library, for an example (follow this link).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Is it possible to store the number as strings and compute them bytes by bytes?

    Or, create a newly defined typed which stores a very large int. let's say 1024 buts instead or 32 bits. Is this possible?
    Last edited by valthyx; 08-06-2011 at 09:10 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is not going to be as capable and fast as an established big number library, but
    yes, you can store your digits as ints or chars, in an array, of either type.

    I used int's and set up 3 arrays. I used one array for the lower number, one array for the upper number, and one array to hold the answer.

    Code:
    1023456677889  <== Upper Number
    4451268039975  <== Lower Number
    =============
    0000000000000  <== Answer
    Then work your calculations through the digits, in a loop. The answer array needs to be sized a bit bigger than the other two arrays, unless you're careful.

    A variable you use for every column, holds any "carry" for the next column, if that should be needed.

    A number is just a representation of a quantity, using a string of digits, so using a string of digits like this, felt very natural. I thought the speed of it was entirely satisfactory, as well. I don't believe it will put GNU's library to shame, however.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by valthyx View Post
    Is it possible to store the number as strings and compute them bytes by bytes?
    Yes, one way of storing a large number is storing it in a string. Operations such as addition are not too hard this way.
    Or, create a newly defined typed which stores a very large int. let's say 1024 buts instead or 32 bits. Is this possible?
    Yes also quite doable, but for this kind of thing it's really a lot nicer if you do it in C++, because then you can do operator overloading, and template the class based on the size. So for my own bigint library, I can give it 1024 as the size and then just treat it largely as I would a normal int.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    What about this, will it work?

    Code:
    struct newint
    {
        char m[2000];
    };
    
    struct newint myNewint1, myNewint2;
    myNewint1.m = "12345";
    myNewint2.m = "11111";
    
    int i;
    for (i = 0; i != '\0'; i++)
    {
      // (multiply)  myNewint1.m[i] * myNewint2.m[i];
    
    }
    I am not sure how to implement the multiplication. Could someone please help me. Thank you.

  7. #7
    1337
    Join Date
    Jul 2008
    Posts
    135
    What about this, will it work?

    Code:
    struct newint
    {
        char m[2000];
    };
    
    struct newint myNewint1, myNewint2;
    myNewint1.m = "12345";
    myNewint2.m = "11111";
    
    int i;
    for (i = 0; i != '\0'; i++)
    {
      // (multiply)  myNewint1.m[i] * myNewint2.m[i];
    
    }
    I am not sure how to implement the multiplication. Could someone please help me. Thank you.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by valthyx View Post
    I am not sure how to implement the multiplication. Could someone please help me. Thank you.
    Think about how you would solve it on paper. It's basic math and hopefully you should have learned it in school.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    He's not kidding. Solve it on paper first. You do implement this algorithm exactly the same way as it is done on paper.

    If that's too hard for you, then start with just incrementing the value (i.e. adding one). Then once you know how to do that, try making it add two numbers. Then once you've got that you can perhaps get the multiplication. You don't have to start with the hardest part.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    It is quite difficult to multiply. For example, I have "1234" and "9694". I would start with
    1. 4*4 = 8 (this is fine)
    2. 9*3 = 27 (i have to bring the 2 to the next multiplication)
    3. 3*6+2 = ....

    These would be very difficult, especially manipulating them in a string. Is there any simpler way?

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by valthyx View Post
    1. 4*4 = 8 (this is fine)
    No it isn't
    These would be very difficult, especially manipulating them in a string. Is there any simpler way?
    All you need is one extra variable to hold the carry over.
    This really is the simplest way, but if you think it is to difficult then maybe you should do something simpler until you get better at C and/or math.
    And who says a char array has to be a string. You can store the digits as 0-9 instead of '0'-'9'. The compiler couldn't care less about it.
    Then when you want to print it add '0' to each digit.
    Last edited by _Mike; 08-07-2011 at 07:31 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by valthyx View Post
    1. 4*4 = 8 (this is fine)
    LOL... CLASSIC .... Care to try that again?

  13. #13
    1337
    Join Date
    Jul 2008
    Posts
    135
    Could anyone please give me an example, just a simple, for me to get an idea of. Thank you.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The concept is just to take each step, just like you would if you were multiplying by hand:

    Code:
    12345
    x     8
    ======
    
    8 * 5 = 40 answer[0] = 0, carry = 4
    8 * 4 = 32 answer[1] = (carry + 2) = 6, carry = 3
    8 * 3 = 24 answer[2] = (carry + 4) = 7, carry = 2
    8 * 2 = 16 answer[3] = (carry + 6) = 8, carry = 1
    8 * 1 = 8  answer[3] = (carry + 8) = 9, carry = 0
    =================================================
    
    answer = 98760
    When your multiplier (bottom number) is two digits or longer, you have to watch your columns in the answer array, more closely. You may even want to use an array for each digit in the multiplier, and then add them up at the last step.

    Since you have to multiply through once for every digit in the multiplier, you need to put the multiplier for loop, as your outer loop in your nested for loops. Your multiplicand loop will be the inner loop:

    Code:
    for(each digit in the multiplier) {  //bottom number loop (multiplier)
       for each digit in the multiplicand) {  top number loop (multiplicand)
          //code for your multiplication goes here
       }
    }

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is a rather fundamental thing that, if you can't multiply two values on paper, you will not be able to describe how to do it either. If you can't describe how to do something, you can't expect to be able to instruct a computer to do it. After all, a computer is just an pedantic ignoramus that will do exactly what your code tells it to do: faithfully, unambiguously, and without question. Your side of that bargain is to give it code that contains precise and meaningful instructions.

    Someone who manages to compute 4*4 as 8 almost certainly has a problem with precision.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a number in an array
    By C++ student in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2009, 08:47 AM
  2. Storing components of a complex number
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-19-2007, 01:37 PM
  3. Long (really long) number
    By Doriän in forum C Programming
    Replies: 5
    Last Post: 09-10-2007, 03:27 PM
  4. Storing long strings?
    By thebudbottle in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 06:10 PM
  5. Storing the whole number
    By flip114 in forum C Programming
    Replies: 3
    Last Post: 10-21-2003, 08:50 AM