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/smith-numbers/smith-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 / smith-numbers / smith-numbers.om` 95 lines ยท 1547 bytes Raw Blame History 1 note Rosetta Code task "Smith numbers": https://rosettacode.org/wiki/Smith_numbers 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text cnt "Smith numbers below 10000: " 7 text first "first 20:" 8 text last "last 10:" 9 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 10 fn emit a view uchar * to void 11 o = ulong 0 12 while lt o len a bound 4096 13 k = write 1 a o sub len a o 14 if le k 0 15 exit 1 16 end 17 o = add o ulong k 18 end 19 end 20 note emitu writes n in decimal; 20 digits hold every ulong 21 fn emitu n ulong to void 22 d = new uchar 20 0 23 i = ulong 20 24 v = n 25 while true bound 20 26 i = sub i 1 27 put d i uchar add rem v 10 48 28 v = div v 10 29 if eq v 0 30 break 31 end 32 end 33 emit part d i sub 20 i 34 end 35 fn dsum n ulong to ulong 36 s = ulong 0 37 v = n 38 while gt v 0 bound 20 39 s = add s rem v 10 40 v = div v 10 41 end 42 ret s 43 end 44 note a Smith number is composite and its digit sum equals the digit sums of its prime factors, with 45 note multiplicity; returns 0 for primes and 1 46 fn smith n ulong to bool 47 v = n 48 d = ulong 2 49 f = ulong 0 50 k = ulong 0 51 while le mul d d v bound 200 52 if eq rem v d 0 53 f = add f dsum d 54 k = add k 1 55 v = div v d 56 else 57 d = add d 1 58 end 59 end 60 if gt v 1 61 f = add f dsum v 62 k = add k 1 63 end 64 if lt k 2 65 ret false 66 end 67 ret eq f dsum n 68 end 69 fn main to int 70 sm = new ulong 400 0 71 c = ulong 0 72 for i ulong 9998 73 n = add i 2 74 if smith n 75 put sm c n 76 c = add c 1 77 end 78 end 79 emit cnt 80 emitu c 81 emit nl 82 emit first 83 for i ulong 20 84 emit sp 85 emitu get sm i 86 end 87 emit nl 88 emit last 89 for i ulong 10 90 emit sp 91 emitu get sm add sub c 10 i 92 end 93 emit nl 94 ret 0 95 end