Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/si/rosetta/tasks/kaprekar-numbers/kaprekar-numbers.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / kaprekar-numbers / kaprekar-numbers.om` 71 lines ยท 1295 bytes Raw Blame History 1 note Rosetta Code task "Kaprekar numbers": https://rosettacode.org/wiki/Kaprekar_numbers 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text hdr "Kaprekar numbers below 10000:" 7 text cnt "count of Kaprekar numbers below `1000000`: " 8 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 9 fn emit a view uchar * to void 10 o = ulong 0 11 while lt o len a bound 4096 12 k = write 1 a o sub len a o 13 if le k 0 14 exit 1 15 end 16 o = add o ulong k 17 end 18 end 19 note emitu writes n in decimal; 20 digits hold every ulong 20 fn emitu n ulong to void 21 d = new uchar 20 0 22 i = ulong 20 23 v = n 24 while true bound 20 25 i = sub i 1 26 put d i uchar add rem v 10 48 27 v = div v 10 28 if eq v 0 29 break 30 end 31 end 32 emit part d i sub 20 i 33 end 34 note n is Kaprekar when n * n splits as a * 10^k + b with b > 0 and a + b = n (1 counts by definition) 35 note bound: n < 10^6, so n * n < 10^12 and p walks at most 12 powers of ten 36 fn kap n ulong to bool 37 if eq n 1 38 ret true 39 end 40 sq = mul n n 41 p = ulong 10 42 while le p sq bound 20 43 b = rem sq p 44 if gt b 0 45 if eq add div sq p b n 46 ret true 47 end 48 end 49 p = mul p 10 50 end 51 ret false 52 end 53 fn main to int 54 emit hdr 55 c = ulong 0 56 for i ulong 999999 57 n = add i 1 58 if kap n 59 c = add c 1 60 if lt n 10000 61 emit sp 62 emitu n 63 end 64 end 65 end 66 emit nl 67 emit cnt 68 emitu c 69 emit nl 70 ret 0 71 end