RosettaCodeData/Task/Kaprekar-numbers/Maple/kaprekar-numbers.maple
2023-07-01 13:44:08 -04:00

26 lines
625 B
Text

isKaprekar := proc(n::posint)
local holder, square, num_of_digits, k, left, right;
holder := true;
if n = 1 then
holder := true;
else
holder := false;
square := n^2;
num_of_digits := length(n^2);
for k to num_of_digits do left := floor(square/10^k);
right := irem(square, 10^k);
if left + right = n and right <> 0 then
holder := true;
break;
end if;
end do;
end if;
return holder;
end proc;
showKaprekar := n -> select(isKaprekar, select(x -> irem(x, 9) = 1 or irem(x, 9) = 0, [seq(1 .. n - 1)]));
countKaprekar := n -> nops(showKaprekar(n));
showKaprekar(10000);
countKaprekar(1000000);