Kaprekar numbers: Difference between revisions

Content added Content deleted
m (whitespace)
Line 18: Line 18:
Example: 10000 (100<sup>2</sup>) splitting from left to right:
Example: 10000 (100<sup>2</sup>) splitting from left to right:
The first split is [1, 0000], which is not OK because "a split of all zeroes is not counted - as zero is not considered positive". Slight optimization opportunity: When splitting from left to right, once the right part becomes all zeroes, you don't need to test this number anymore because its splits will always be invalid.
The first split is [1, 0000], which is not OK because "a split of all zeroes is not counted - as zero is not considered positive". Slight optimization opportunity: When splitting from left to right, once the right part becomes all zeroes, you don't need to test this number anymore because its splits will always be invalid.



=={{header|C}}==
=={{header|C}}==
Line 301: Line 300:
=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==


<lang Icon>
<lang Icon>procedure is_kaprekar (n)
procedure is_kaprekar (n)
if n = 1 then {suspend n; return}
if n = 1 then {suspend n; return}


Line 322: Line 320:
if is_kaprekar (i) then count +:= 1
if is_kaprekar (i) then count +:= 1
write ("Number of Kaprekar numbers less than 1000000 is ", count)
write ("Number of Kaprekar numbers less than 1000000 is ", count)
end
end</lang>
</lang>


Output:
Output:
Line 468: Line 465:


# one million is a small number, let's brute force it
# one million is a small number, let's brute force it
is_kaprekar($_) and print "$_\n" for 1 .. 1_000_000;
is_kaprekar($_) and print "$_\n" for 1 .. 1_000_000;</lang>
</lang>
Output:<pre>1
Output:<pre>1
9
9