Do you know where your variables are? PerlMonks

Comment on

 Need Help??

Ever since I've read about them, I've been fascinated by spigot algorithms for producing digits of pi.

The basic idea for those algorithms is that most "interesting" transcendental numbers (like pi, e, ln(2)) have a pretty simple representation if the base is allowed (in a regular pattern) for each digit. Then the task of computing the first \$N first digits is just that of a base conversion. And the fascinating part is that you can work with integers only-

To stay a bit on topic, I've ported this C implementation of a spigot algorithm for pi to Perl 6:

```sub pi-spigot(Int \$digits) {
my \$len = 1 + floor 10 * \$digits / 3;
my @a = 2 xx \$len;
my Int \$nines    = 0;
my Int \$predigit = 0;
join '', gather for 1 .. (\$digits + 1) -> \$j {
my Int \$q = 0;
loop (my int \$i = \$len; \$i > 0; \$i = \$i - 1) {
my int \$x = 10 * @a[\$i - 1] + \$q * \$i;
@a[\$i - 1] = \$x % ( 2 * \$i - 1);
\$q = \$x div (2 * \$i - 1);
}

@a[0] = \$q % 10;
\$q div= 10;
if \$q == 9 {
++\$nines;
}
elsif \$q == 10 {
take \$predigit + 1, 0 xx \$nines;
\$nines    = 0;
\$predigit = 0;
}
else {
take \$predigit;
\$predigit = \$q;
take 9 xx \$nines;
\$nines = 0;
}
}
}

multi MAIN(\$n = 100) {
say pi-spigot(\$n.Int);
}

multi MAIN('test') {
use Test;
plan 1;
is pi-spigot(100),
'03141592653589793238462643383279502884197169399375105820974944
+59230781640628620899862803482534211706',
'it works';
}

Since Rakudo is still pretty slow for this kind of stuff, I've traded a bit of readabilty for speed by using a native int in the inner loop, which means that Rakudo can inline most operators, but means I have to write \$i = \$i - 1 instead of \$i-- (because native ints are value types, and you cannot (yet?) pass them as writable values to routines, so the -- operator cannot work on them).

In reply to Re: Computing pi to multiple precision by moritz
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":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-08-23 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (357 votes). Check out past polls.

Notices?