Kaprekar numbers
String version (slow)
sub kaprekar( Int $n ) {
my $sq = $n ** 2;
for 0 ^..^ $sq.chars -> $i {
my $x = +$sq.substr(0, $i);
my $y = +$sq.substr($i) || return;
return True if $x + $y == $n;
}
False;
}
print 1;
print " $_" if .&kaprekar for ^10000;
print "\n";Output:
1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999Numeric version (medium)
sub kaprekar( Int $n, Int :$base = 10 ) {
my $hi = $n ** 2;
my $lo = 0;
loop (my $s = 1; $hi; $s *= $base) {
$lo += ($hi % $base) * $s;
$hi div= $base;
return $hi,$lo if $lo + $hi == $n and $lo;
}
();
}
print " $_" if .&kaprekar for ^10_000;
my atomicint $n;
(^1_000_000).race.map: { $n++ if kaprekar $_ }
say "\n\nBase 10 Kaprekar numbers < :10<1_000_000> = $n";
say "\nBase 17 Kaprekar numbers < :17<1_000_000>";
my &k17 = &kaprekar.assuming(:base(17));
my @results;
(^:17<1_000_000>).race.map: -> $n {
my ($h,$l) = k17 $n;
next unless $l;
my $n17 = $n.base(17);
my $s17 = ($n * $n).base(17);
my $h17 = $h.base(17);
@results.push: "$n $n17 $s17 ($h17 + $s17.substr(* - max(1,($s17.chars - $h17.chars))))";
}
.say for @results.sort: *.chars;Output:
Note that this algorithm allows the null string on the left, taken as zero, which automatically includes 1 as the first element of the sequence.
Casting out nines (fast)
(Same output.)
Last updated
Was this helpful?