You are not logged in.

#1 2009-12-28 16:19:00

MrAllan
Member
Registered: 2008-12-08
Posts: 132

[SOLVED] Large numbers in C

Hi all,

I'm writing a program that deals with _very_ large numbers. Specifically, I want to represent at least 25! (which is bigger than 10^25). I have already tried out the unsigned long long int, but it is not big enough.

Thanks for any help!

- MrAllan

Last edited by MrAllan (2009-12-29 13:36:08)

Offline

#2 2009-12-28 16:42:24

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: [SOLVED] Large numbers in C

Hi MrAllan,

I've needed this once (for Project Euler) and I used http://gmplib.org (pacman -S gmp) It was reasonably easy to use and worked quite fast. (I think Haskell uses it too for its Integer implementation.)

Offline

#3 2009-12-29 01:51:05

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: [SOLVED] Large numbers in C

wuischke wrote:

I've needed this once (for Project Euler) and I used http://gmplib.org (pacman -S gmp)

ditto. I used it to calculate pi, and its pretty simple in C++

Offline

#4 2009-12-29 13:16:39

MrAllan
Member
Registered: 2008-12-08
Posts: 132

Re: [SOLVED] Large numbers in C

wuischke wrote:

Hi MrAllan,

I've needed this once (for Project Euler) and I used http://gmplib.org (pacman -S gmp) It was reasonably easy to use and worked quite fast. (I think Haskell uses it too for its Integer implementation.)

Thanks for the answer! I tried out GMP, but I get an error:

"___gmpz_init", referenced from: _main in main.o
Symbol(s) not found
Collect2:ld returned 1 exit status

Here's my code:

#include <stdio.h>
#include </usr/include/gmp.h>

int main (int argc, const char * argv[]) {
    mpz_t integ;
    mpz_init(integ);
    return 0;
}

What am I doing wrong?

Offline

#5 2009-12-29 13:27:17

zukka
Member
From: Bologna Italy
Registered: 2006-06-27
Posts: 26

Re: [SOLVED] Large numbers in C

What am I doing wrong?

You probably aren't linking the library during the compilation.
You have to pass -lgmp to gcc

[andrea@consistemi gmptest]$ gcc test.c
/tmp/cc0ucLbR.o: In function `main':
test.c:(.text+0x11): undefined reference to `__gmpz_init'
collect2: ld returned 1 exit status
[andrea@consistemi gmptest]$ gcc -lgmp test.c
[andrea@consistemi gmptest]$ ./a.out

Also the include is not correct

#include </usr/include/gmp.h>  // should be #include <gmp.h>

Offline

#6 2009-12-29 13:35:49

MrAllan
Member
Registered: 2008-12-08
Posts: 132

Re: [SOLVED] Large numbers in C

zukka wrote:

Also the include is not correct

#include </usr/include/gmp.h>  // should be #include <gmp.h>

I actually had just "#include <gmp.h>" before, but then it would muck about how it couldn't find "no such file or directory".
Whatever, now it works, so thanks a lot!

Offline

Board footer

Powered by FluxBB