C++ code will not compile
Jonathan A. Zylstra
jon at jzylstra.com
Mon Mar 6 10:07:16 CET 2006
Greg Davis wrote:
>Blake,
>
>Thanks for the reply, the code compiles now but it segfaults after a few
>seconds.
>
>I guess maybe I'll have to buy a new computer unless somebody here has a
>better idea. :P
>
>
>
Well, I recommend a computer with at least. ~ 8.5 GB of memory that can
be devoted exclusively to that program.
It segfaults because you have run out of memory ... here's why:
No. of decimal digits of your number:
(126976 ^ 4294967295) = (10 ^ x)
Log[base 10] (126976 ^ 4294967295) = Log[Base 10](10 ^ x)
4294967295 * Log [b10] (126976) = x * Log[b10] (10)
21920317535 = x, number of decimal digits [yes, nearly 22 billion ]
to convert to base 2, multiply by (log[b10] (10) / log[b10] (2) ) ~= 3.3219
Which gives you [about] 72817718668 bits , which is about 8.5 GB.
Plus there is some overhead for the mpz_t class ... keeping track of all
that memory!
It looks small from an algebraic standpoint on paper ... but that's a
huge number.
Try some interesting numbers like (2^3021377)-1 ... only about 368kb of
memory. [see www.mersenne.org for that number ...]
Jonathan A. Zylstra
>Thanks for any further suggestions.
>
>
>
>Blake Huff wrote:
>
>
>
>>Gregg:
>>
>>I assume it's because 2^32 = 4294967296, which is too large as the
>>range of an unsigned long integer should be 0-4294967295. If it
>>compiled correctly, it would have to be done on a 64 bit system (or
>>something larger than 32 bits).
>>
>>Blake
>>
>>
>>P.S. To get the same effect, try this:
>>
>>mpz_set_ui(x, 4294967295);
>>mpz_add_ui(x, x, 1);
>>
>>It might take an extra computational cycle or two, but that's why we
>>make ghz processors, no?
>>
>>
>>
>>
>>
>>On Mar 5, 2006, at 11:47 AM, Greg Davis wrote:
>>
>>
>>
>>
>>
>>>Hi guys,
>>>
>>>I got this code from some forums:
>>>
>>>#include <iostream>
>>>using namespace std;
>>>#include <gmp.h>
>>>
>>>int main()
>>>{
>>>mpz_t x;
>>>unsigned int y=126976;
>>>mpz_init(x);
>>>mpz_set_ui(x,4294967296);
>>>cout << "\nx: " <<x << endl;
>>>cout << "\ny: " <<y << endl ;
>>>mpz_pow_ui(x, x, y);
>>>cout << "x^y is " << x << endl;
>>>mpz_clear(x);
>>>}
>>>
>>>but when I try to compile it I get:
>>>
>>>bignum.cpp:11: error: integer constant is too large for "long" type
>>>
>>>Can anyone help me figure out why it does this? It seems to compile
>>>fine
>>>on his computer.
>>>
>>>Thanks for any help.
>>>_______________________________________________
>>>gmp-discuss mailing list
>>>gmp-discuss at swox.com
>>>https://gmplib.org/mailman/listinfo/gmp-discuss
>>>
>>>
>>>
>>>
>>Blake Huff
>>stangmechanic at gmail.com
>>
>>
>>
>>_______________________________________________
>>gmp-discuss mailing list
>>gmp-discuss at swox.com
>>https://gmplib.org/mailman/listinfo/gmp-discuss
>>
>>
>>
>>
>>
>
>_______________________________________________
>gmp-discuss mailing list
>gmp-discuss at swox.com
>https://gmplib.org/mailman/listinfo/gmp-discuss
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3166 bytes
Desc: S/MIME Cryptographic Signature
Url : http://gmplib.org/list-archives/gmp-discuss/attachments/20060306/f3c8d7d2/smime.bin
More information about the gmp-discuss
mailing list