Handling big numbers in C

This is a discussion on Handling big numbers in C within the C Programming forums, part of the General Programming Boards category; Hey, I've been searching for a way of handling big numbers in C for a long time now... I want ...

  1. #1
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91

    Handling big numbers in C

    Hey, I've been searching for a way of handling big numbers in C for a long time now... I want to handle numbers up to a trillion.

    I have found the gmp library, which seems to do exactly what I want, but my compiler (Digital Mars) dosen't have it by default. I went to the gmp website and downloaded the zip file containaing the library but I don't know how to install it... I searched all over the net for a tutorial on how to install the library and found a few of them, but I don't understand the instructions... They seem to use some Unix comands while installing the library =S

    So any help at installing the gmp library or pointing me towards another way of handling big numbers in C would be deeply appreciated.

    Thanks in advance =)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Are you using Linux? If so, which distro?

    Edit: I'm confused, because some of your other posts suggest that you're using Windows, and I don't see any zip files on the GMP website, only tarballs.
    Last edited by robatino; 07-04-2007 at 07:41 PM.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I have used gmp, please specify where do you stop in the installation.

  4. #4
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    robatino

    Sorry, I said zip when they're infact .tar.gz files... I forgot that.

    I use windows, and thought that .tar.gz files could also be used in windows.

    Mortissus

    I just download the file containing the library, didn't know what to do after that.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Looking through the gmp-discuss mailing list archives (http://gmplib.org/#MAILINGLISTS), I found this:

    Native Windows Binaries for GMP-4.2.1

  6. #6
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    robatino wich of these files should I download? There are a lot of them =(

    I'm using Digital Mars.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'm not familiar with Windows, but you could just download the pre-built binaries for your system, whatever that is.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Your compiler doesn't give you a 64 bit long long type?

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    I had an assignment once about this subject. I used linked lists to do the job, the huge integer was given from a file or from stdin and then firstly i saved every digit in nodes including the sign and after this i created add,sub, mul, div, mod etc. I have writen to many lines of code, i advise you to make a given library work as its a hard thing to do.

  10. #10
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    brewbuck

    Yes it does, but when I said a trillion I meant much bigger numbers...

    I'm doing Project Euler and a lot of it's challenges involves dealing with ridiculously big numbers... Such as this one:



    (I don't if even gmp could handle 28433 * 2^7830457+1, there's probably a smarter way for calculating it's last ten digits, but there are other problems wich for example require you to calculate the max digital sum of a^b where a, b < 100.....)

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Lala if you if your using Digital mars, in order to use the GMP you need copying the lib file to the Lib folder under \dm\lib and copy the header file on to the \dm\include folder. And that then include the header in your main code.

    Hope you can star using the library.

    ssharish2005

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > you need copying the lib file to the Lib folder under \dm\lib
    > and copy the header file on to the \dm\include folder
    Doesn't DM support the usual -I and -L compiler flags for specifying additional places to find include files and libraries?

    Messing with the standard implementation directories isn't a good idea IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > I don't if even gmp could handle 28433 * 2^7830457+1, there's probably a smarter way for calculating it's last ten digits,

    If all you want is the last 10 digits, you can compute using modular arithmetic, reducing modulo 10^10 as you go:

    http://en.wikipedia.org/wiki/Modular_arithmetic

    So you can first compute the last 10 digits of 2^7830457 by successively doubling, lopping off any digits past the last 10 as you go, so you never have more than 11 digits. Then take the resulting at most 10 digit number, multiply by 28433, then take the last 10 digits of that. Finally, add 1 and take the last 10 digits of that. Intuitively, it should be pretty obvious why this works, even if you don't know about modular arithmetic.

    Edit: Since doubling is cheap (bitwise left shift) and reducing mod 10^10 is expensive, for efficiency you would double as many times as you could before exceeding your machine's capacity, before reducing. You could also avoid the multiplication by 28433, by starting with 28433 and doubling it 7830457 times (reducing as you go).
    Last edited by robatino; 07-05-2007 at 06:45 AM.

  14. #14
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Sorry for the delay. To install GMP try this:

    tar xvf gmp-4.2.1.tar.gz
    cd gmp-4.2.1
    ./configure
    make
    make install

    Good luck, any problems just ask

  15. #15
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    robatino

    I've tried following your instructions but my code didn't work... Am I on the right track?

    Code:
    #include <stdio.h>
    
    int main ()
    {
        unsigned long long number = 28433;
    
        for (int i = 0; i <= 7830457; i++)
        {
            number = (number << 1) &#37; 10000000000;
        }
    
        number++;
    
        printf ("%d",  number);
        return 0;
    }
    ssharish2005

    I've copied all the .lib files to the lib directory and all .h files to the include directory but when I tried to compile a code that was in project euler's forum I get a bunch of errors...

    Code:
     Error 42: Symbol Undefined ___gmpz_clear
     Error 42: Symbol Undefined ___gmpz_get_str
     Error 42: Symbol Undefined ___gmpz_add
     Error 42: Symbol Undefined ___gmpz_ui_pow_ui
     Error 42: Symbol Undefined ___gmpz_init
    Inside the zip file was a dll file wich I didn't copy to any folders... Maybe the error is there.

    Mortissus

    I don't know where and how to use these comands

    E &#233; sempre bom encontrar um brasileiro por ai =)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Big (and small) numbers...
    By swif in forum C Programming
    Replies: 6
    Last Post: 04-22-2005, 12:21 PM
  4. Using very big numbers, long isn't enough
    By Zewu in forum C++ Programming
    Replies: 9
    Last Post: 03-27-2005, 06:54 AM
  5. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21