in reply to Incrementing a large number without Math::BigInt

How big is big, and how slow is slow. Is there a limit to how big the numbers are you wish to handle? How about some sample code and an indiaction of how long it takes to execute on your system and how much faster you require it to go?


DWIM is Perl's answer to Gödel
  • Comment on Re: Incrementing a large number without Math::BigInt

Replies are listed 'Best First'.
Re^2: Incrementing a large number without Math::BigInt
by Cap'n Steve (Friar) on Apr 26, 2006 at 02:51 UTC
    I'd like it to go as fast as possible and not restrict myself to a certain size, if possible. Doing some tests on my 400mhz laptop, it takes about 5 minutes to create a number with 10 million digits. I haven't tried incrementing it at all.
    use Math::BigInt; my $i = 10000000; while (1) { my $num = Math::BigInt->new('1' . '0' x $i); print $i . "\n"; $i++; }
    Update: As a side note, Math::BigInt has some really weird memory usage. It jumps around like crazy, but my first test used about 50 megabytes on average while the second uses about 160.

      You may be interested that:

      use strict; use warnings; my $i = 100000000; my $num = '9' x $i; $num++; print length ($num) . ' ' . substr $num, 0, 10;

      Prints:

      100000001 1000000000

      and executes in about 1 second.


      DWIM is Perl's answer to Gödel
        Wow, that is impressive. Silly me, not even trying the obvious way because I thought Perl couldn't handle it. I've learned my lesson.

        Just curious, what other numerical operations can Perl handle as a string that it can't handle as a number? Deincrementing doesn't seem to work.