interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / kaprekar-numbers.wiki

79 lines · 1567 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text hdr "Kaprekar numbers below 10000:"
9text cnt "count of Kaprekar numbers below 1000000: "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note n is Kaprekar when n * n splits as a * 10^k + b with b > 0 and a + b = n (1 counts by definition)
37note bound: n < 10^6, so n * n < 10^12 and p walks at most 12 powers of ten
38fn kap n ulong to bool
39if eq n 1
40ret true
41end
42sq = mul n n
43p = ulong 10
44while le p sq bound 20
45b = rem sq p
46if gt b 0
47if eq add div sq p b n
48ret true
49end
50end
51p = mul p 10
52end
53ret false
54end
55fn main to int
56emit hdr
57c = ulong 0
58for i ulong 999999
59n = add i 1
60if kap n
61c = add c 1
62if lt n 10000
63emit sp
64emitu n
65end
66end
67end
68emit nl
69emit cnt
70emitu c
71emit nl
72ret 0
73end
74</syntaxhighlight>
75{{out}}
76<pre>
77Kaprekar numbers below 10000: 1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999
78count of Kaprekar numbers below 1000000: 54
79</pre>