Kaprekar numbers

Post Reply
User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Kaprekar numbers

Post by Mr. Kibernetik »

This program calculates Kaprekar numbers:

Code: Select all

kap,n = getkap(10000)
> i, 1..n
  #.output(kap[i])
<

getkap(x)=
  > k, 1..x
    n = #.lower(#.log10(k^2))+1
    > i, 1..n
      r = k^2%10^i
      << r>k
      >> r=0
      l = #.lower(k^2/10^i)
      ? r+l=k, kap[#.size(kap,1)+1] = k
    <
  <
  <= kap,#.size(kap,1)
.
Output:

Code: Select all

1
9
45
55
99
297
703
999
2223
2728
4879
4950
5050
5292
7272
7777
9999

Post Reply