Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I found a nice Perl 5 implementation of a spigot algorithm to generate pi at http://rosettacode.org/wiki/Pi#Perl. With a bit of tweaking, I was able to convert this code into a true spigot: a function which returns a further sequence of digits on each successive call:

#! perl use strict; use warnings; use Math::BigInt try => 'GMP'; use bigint; { local $| = 1; my $digits = int($ARGV[0] // 0) || 60; my ($len, $d) = rosetta(); printf "pi = %s.%s", substr($d, 0, 1), substr($d, 1); for (my $count = $len; $count < $digits; $count += $len) { ($len, $d) = rosetta(); print $d; } print ";\n"; reset_rosetta(); ($len, $d) = rosetta(); printf "pi = %s.%s", substr($d, 0, 1), substr($d, 1); } BEGIN { my ($ds, $ns, $n5, $d5, $n2, $d2, $p2, $pow, $x); reset_rosetta(); sub reset_rosetta { $ds = 1; $ns = 0; $n5 = 1_184; $d5 = 375; $n2 = 685_448; $d2 = 40_955_757; $p2 = 5; $pow = 1; $x = 5; } sub rosetta { my $out; for (my $ppow = 1; $ppow == 1; $x += 4) { $ns = ($ns * $d5) + ($n5 * $pow * $ds); $ds *= $d5; $n5 = 16 * (25 * ($x + 2) - $x); next_term($d5, 5, $x); while ($d5 > $d2) { $ns = ($ns * $d2) - ($n2 * $pow * $ds); $ds *= $d2; $n2 = 4 * (57_121 * ($p2 + 2) - $p2); next_term($d2, 239, $p2); $p2 += 4; } my $product1 = $n5 * 625; my $product2 = $n2 * $n2 * 3_262_808_641; $ppow = 1; while ($pow * $product1 < $d5 && $pow * $product2 < $d2) { $pow *= 10; $ppow *= 10; } if ($ppow > 1) { $ns *= $ppow; $out = $ns / $ds; $ns %= $ds; $out = ('0' x (length($ppow) - length($out) - 1)) . $o +ut; } if ($p2 % 20 == 1) { my $g = Math::BigInt::bgcd($ds, $ns); $ds /= $g; $ns /= $g; } } return (length $out, $out); } } sub next_term { my ($coef, $p) = @_[1, 2]; $_[0] /= ($p - 4) * ($p - 2); $_[0] *= $p * ($p + 2) * $coef ** 4; }

Some advantages of this approach:

  • The output is in decimal.
  • The output can be displayed progressively, so that, for a large number of digits, the user can ‘see’ that progress is being made.
  • With use of the GMP library, performance is surprisingly fast (10,000 digits in under a minute).
  • Flexibility: the caller is free to display or otherwise use the data returned as required.

Let me emphasize, the code is not mine, I have only massaged it into a (hopefully) more useful form. Perhaps it will prove interesting or helpful to others exploring this topic.

Athanasius <°(((><contra mundum


In reply to Re^2: Computing pi to multiple precision by Athanasius
in thread Computing pi to multiple precision by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found