On 2020-05-14 08:13, Bruce Gray wrote:


On May 14, 2020, at 7:27 AM, ToddAndMargo via perl6-users 
<perl6-us...@perl.org> wrote:

Hi All,

1) how do I get 40 or more digits out of sqrt?

—snip—

Use an Integer Root algorithm on ($number-you-want-the-root-of * 100 ** 
$number-of-digits-you-want), then shift the decimal point of the result.

Exact code here:
        https://rosettacode.org/wiki/Integer_roots#Raku

—
Hope this helps,
Bruce Gray (“Util” on RosettaCode, too)


sub integer_root ( Int $p where * >= 2, Int $n --> Int ) {
    my Int $d = $p - 1;
    my $guess = 10**($n.chars div $p);
    my $iterator = { ( $d * $^x   +   $n div ($^x ** $d) ) div $p };
    my $endpoint = {      $^x      ** $p <= $n
                     and ($^x + 1) ** $p >  $n };
    min (+$guess, $iterator ... $endpoint)[*-1, *-2];
}

say integer_root( 2, 2 * 100 ** 2000 );



It does help!  I can reproduce noise to my heart's content!

Questions:

what is $p in the sub declaration? Is it always a 2?

what is `$n` in the sub declaration?  Looks like
the third set of numbers is the digits I want.

what is the second number (100)?

And what is `2 * 100 ** 2000 `?  Is that `(2 x 100)^ 2000`
((2 times 100) to the 2000 power?

> say integer_root( 2, 2 * 100 ** 4 );
14142

> say 3.sqrt
1.7320508075688772

> say integer_root( 2, 3 * 100 ** 16 );
17320508075688772


-T

Reply via email to