robert44444uk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Pearl Monks, I'm back twice in 2 days. I managed to get a program to work which pleases me no end, but now I face some issue relating to large integers (I think). The following code breaks down very soon after it starts. It had run happily all day long with small input $beg and $end amounts. The integers being handled are about 10^20, so I would have thought big float would be happy.

#!/usr/bin/env perl use warnings; use strict; use Math::BigFloat lib=>"GMP"; use Math::Prime::Util qw/:all/; use feature ':5.10'; use File::Slurp; my @crt = (68083089690,131417981310,36415643880,51194848320,1061578864 +50,30083217780,154569039240,55922110860,18269362650,12688113900,10860 +8348310,2163950250,195808561410,89629478400,35610139950,164950347870, +104015130450,198396537570,4751926410,91952139510,187872373920,1822911 +25170,144638376960,149365639500,45991448580,94402601370,142813754160, +69142506510,132477398130,164144843940); my $beg = shift || 91960283; my $end = shift || 300000000; my $fact = 2*3*5*7*11*13*17*19*23*29*31; my $target = 650; foreach my $i ($beg .. $end) { foreach my $a (@crt) { my $num = $fact*$i+$a; my $start = prev_prime($num); my $end = next_prime($num); my $gap = $end - $start; if ($gap> $target) { say $i, " ", $a, " ", $gap," ",$gap/log($num); } } }

Replies are listed 'Best First'.
Re: Odd error message
by shadowsong (Pilgrim) on Sep 15, 2015 at 00:59 UTC

      Thank you shadowsong. I tried this on my batch, but it did not terminate after 1 day, suggesting a massive loss of efficiency (it should have completed in 30 minutes). Also some of my results have NaN, so I will have to look into that. But I can confirm the program did not crash!

        hmm, the NaN problem is a known problem when running Strawberry Perl sadly with this particular package - related to the log function

        robert44444uk,

        No problem. I had another crack at it using what I thought would entail slightly less overhead:

        However, it performed just as bad as your attempts using next_prime and prev_prime if not worse...

        We'd be very keen to see how/if you managed to get your script to perform better.

        Best of luck (and God speed)
        shadowsong

Re: Odd error message
by ww (Archbishop) on Sep 14, 2015 at 21:26 UTC

    Had you included (inside code tags just as you enclosed your script) the "odd error message" instead of expecting us to intuit ("code breaks down") just what errors you're getting, you would have made it much easier for us to help you.

    Please review On asking for help, How do I post a question effectively? and I know what I mean. Why don't you?

    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
      thank you ww I do wish I knew how to copy error messages without having to write them down. I use strawberry perl, and the console gives the error message without an opportunity to copy it. I just assumed that if anyone ran the code they would get the same message. it reads something like: next_prime <n> : input '1.84467440869813e+019' must be a positive integer at c:\subdirectory... line 20

        This is a pretty sketchy reply, but please forgive that as it's been a long time since I set up a "Command Prompt" to allow highlighting a word, phrase, para or whatever, so that by hitting RETURN (aka ENTER) I dump the highlighted data into a buffer which can then be pasted into another application -- say, my text editor, a calc sheet, etc.

        Laurent_R describes an ad hoc approach but you can make your Win32 console more manageable (editable) by opening a "Command Prompt"; right clicking on the title bar (including the icon at the top left which is inside the bar) and adjusting the various options under "Properties."

        Addtl thoughts: check the quickedit and insert boxes in the first tab (options), and, often, you'll find it useful to increase the window and screenbuffer dimensions in the "layout" tab.

        Edit 2: This is from a Win7 Pro box:

        This is perl 5, version 18, subversion 4 (v5.18.4) built for MSWin32-x86-multi-thread-64int
        (with 1 registered patch, see perl -V for more detail)
        
        Copyright 1987-2013, Larry Wall
        
        Binary build 1805 299195 provided by ActiveState http://www.ActiveState.com
        Built Jul 20 2015 16:07:57
        I (eventually) see this message, error and output:

        1141975.pl
        Math::BigInt: couldn't load specified math lib(s), fallback to Math::BigInt::Calc at 1141975.pl line 4.
        
        Parameter '1.84467440869813e+019' must be a positive integer at 1141975.pl line 20.
        

        ++$anecdote ne $data

        input '1.84467440869813e+019' must be a positive integer

        A rather odd message, given that '1.84467440869813e+019' is a positive integer value.
        I think next_prime() must be looking for an IV or UV, but the value about which it complains is too large to fit into an IV/UV and has been converted to an NV (double precision float).

        I can't quite reconcile the error message you've provided with the Math::Prime::Util (version 0.53) source.
        Perhaps I was looking in the wrong place, or perhaps I was looking at the wrong version.
        If you're not already using 0.53, consider upgrading to it - it might accept such values (dunno).

        Also note that, although your script loads the Math::BigFloat module, I can't see any Math::BigFloats being used anywhere in your code.
        And I don't think next_prime() would accept a Math::BigFloat input, anyway - though I haven't tested.

        Cheers,
        Rob
        Yeah, the Windows cmd console is very primitive, indeed. You can still copy and paste: click on the icon at the top left corner of the console windows, and you get a menu. Chose 'modify' and you get a sub-menu in which you can select, copy and paste, very unpractical, but workable.

        On your original question, I am not very clear on what your problem is, but maybe you should consider bigint or Math/BigInt rather than Math::BigFloat.

Re: Odd error message
by danaj (Friar) on Sep 15, 2015 at 21:14 UTC

    MPU is very strict about getting integers, as it is very easy for Perl to suddenly decide a large number is a float. This is almost always something Very Bad when doing number theory. I chose to have it complain instead of giving subtly wrong results.

    One of the problems here is the merit calculation needs to be done using FP logs. This is why, if you look at my gap software, everything is a bigint except the merit calculation, done in a sub that explicitly converts to BigFloats. It's a PITA, and Perl6 / PARI are much nicer in this.

    Anyway, $fact*$i could easily overflow (primorial(31)*3e8 is larger than 64-bit). You need to do something like my $fact = Math::BigInt->new(primorial(31)); to force $num to be a bigint. Then the merit calc should be done as a sub that uses Math::BigFloat.