You guys, I wrote a perl program to calculate the 13 million digits of 2**43_112_609-1:

use bignum only => "GMP"; # GMP added later use strict; my $x = 43_112_609; warn "x isa " . ref($x). "!\n"; my $y = ( (2**$x) - 1 ); open my $out, ">", "huge.txt" or die $!; print $out "huge prime: ", $y, "\n";

I mean, I know bignum isn't exactly news, but I just think it's a very cool package.

ref: daily galaxy: ultimate-prime.html

-Paul

Replies are listed 'Best First'.
Re: bignum rocks 2^43_112_609-1
by JavaFan (Canon) on Sep 29, 2008 at 14:30 UTC
    Unfortunally, bignum is also kind of slow. Take the following program:
    use 5.010; use bignum; use warnings; use strict; my $x = shift || 100; my $y = 2**$x - 1; say $y;
    calling it with 500000 as argument took 3m02s on my box (output redirected to /dev/null). The equivalent
    echo '2^500000-1'|bc > /dev/null
    only took 4 seconds.

    So, in general, if I want arbitrary precision numbers, I use bc instead of bignum.

      By default, bignum uses a backend module written in pure Perl to do the math, but you can instruct it to use a different implementation, for instance the fast GMP library (see this):

      On my computer:

      $ time echo '2^500000-1'|bc >/dev/null real 0m3.057s user 0m2.920s sys 0m0.010s $ time perl -e 'use bigint only => 'GMP'; my $x = 2 ** 500000 - 1; pri +nt $x' >/dev/null real 0m0.201s user 0m0.160s sys 0m0.010s
      In that case, the perl version is ~15 times faster!!!
        See, I'm under the impression that bignum uses Math::BigInt, which uses Math::BigInt::GMP if available. Since I have Math::BigInt::GMP installed, I'd assume (by default) it'd use that. But otherwise, I see what you mean.

        It seems reasonable to me to have bigint require GMP. I wonder if there are cases where you'd really want to do bignum processing in pure Perl.

        UPDATE: this is false. bignum explicitly asks for ::Calc. Why would it do that?

        -Paul

      Wow, that's a huge difference. Is this a GMPlib problem or a bignum.pm problem? That's the question in my mind.

      -Paul

Re: bignum rocks 2^43_112_609-1
by Anonymous Monk on Sep 29, 2008 at 12:18 UTC
    Like pouring liquid nitro on some beans, cool :D
Re: bignum rocks 2^43_112_609-1
by bart (Canon) on Oct 06, 2008 at 14:44 UTC
    print $out "huge prime: ", $y, "\n";
    Here's to hoping it is a prime.

    update: Well, ambrus claims it must be, pointing towards this.

      My original post totally links to an article about 2**43_112_609-1.

      -Paul

Re: bignum rocks 2^43_112_609-1
by swampyankee (Parson) on Oct 07, 2008 at 02:17 UTC

    My copy of maxima dies. 8-(

    (

    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc