Jump to content

Integer roots: Difference between revisions

m
→‎{{header|Perl 6}}: generate initial guess numerically
m (→‎{{header|Perl 6}}: generate initial guess numerically)
Line 615:
<lang perl6>sub integer_root ( Int $p where * >= 2, Int $n --> Int ) {
my Int $d = $p - 1;
my $guess = '1' ~ ( '0' x 10**($n.chars /div $p) );
 
my $guess = '1' ~ ( '0' x ($n.chars / $p) );
 
my $iterator = { ( $d * $^x + $n div ($^x ** $d) ) div $p };
 
my $endpoint = { $^x ** $p <= $n
and ($^x + 1) ** $p > $n };
 
return [min] (+$guess, $iterator ... $endpoint)[*-1, *-2];
}
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.