How to resolve the algorithm Kaprekar numbers step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Kaprekar numbers step by step in the Raku programming language
Table of Contents
Problem Statement
A positive integer is a Kaprekar number if: Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.
10000 (1002) splitting from left to right:
Generate and show all Kaprekar numbers less than 10,000.
Optionally, count (and report the count of) how many Kaprekar numbers are less than 1,000,000.
The concept of Kaprekar numbers is not limited to base 10 (i.e. decimal numbers); if you can, show that Kaprekar numbers exist in other bases too.
For this purpose, do the following:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kaprekar numbers step by step in the Raku programming language
Source code in the raku programming language
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";
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;
sub kaprekar-generator( :$base = 10 ) {
my $base-m1 = $base - 1;
gather loop (my $place = 1; ; ++$place) {
my $nend = $base ** $place;
loop (my $n = $base ** ($place - 1); $n < $nend; ++$n) {
if $n * ($n - 1) %% $base-m1 {
my $pend = $place * 2;
loop (my $p = $place; $p < $pend; ++$p) {
my $B = $base ** $p;
my $lo = $n * ($B - $n) div ($B - 1);
my $hi = floor $n - $lo;
if $n * $n == $hi * $B + $lo and $lo {
take [$n, $hi, $lo];
last;
}
}
}
}
}
}
print " $_[0]" for kaprekar-generator() ...^ *.[0] >= 10_000;
say "\n";
say "Base 10 Kaprekar numbers < :10<1_000_000> = ", +(kaprekar-generator() ...^ *.[0] >= 1000000);
say '';
say "Base 17 Kaprekar numbers < :17<1_000_000>";
my &k17-gen = &kaprekar-generator.assuming(:base(17));
for k17-gen() ...^ *.[0] >= :17<1000000> -> @r {
my ($n,$h,$l) = @r;
my $n17 = $n.base(17);
my $s = $n * $n;
my $s17 = $s.base(17);
my $h17 = $h.base(17);
my $l17 = $l.base(17);
$l17 = '0' x ($s17.chars - $h17.chars - $l17.chars) ~ $l17;
say "$n $n17 $s17 ($h17 + $l17)";
}
You may also check:How to resolve the algorithm Compare length of two strings step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Java programming language
You may also check:How to resolve the algorithm Ramanujan's constant step by step in the Python programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Fortran programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Wisp programming language