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-rosetta/blob/main/drafts/rosettacode/smith-numbers.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / smith-numbers.wiki`
104 lines ยท 1854 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text cnt "Smith numbers below 10000: "
9 text first "first 20:"
10 text last "last 10:"
11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12 fn emit a view uchar * to void
13 o = ulong 0
14 while lt o len a bound 4096
15 k = write 1 a o sub len a o
16 if le k 0
17 exit 1
18 end
19 o = add o ulong k
20 end
21 end
22 note emitu writes n in decimal; 20 digits hold every ulong
23 fn emitu n ulong to void
24 d = new uchar 20 0
25 i = ulong 20
26 v = n
27 while true bound 20
28 i = sub i 1
29 put d i uchar add rem v 10 48
30 v = div v 10
31 if eq v 0
32 break
33 end
34 end
35 emit part d i sub 20 i
36 end
37 fn dsum n ulong to ulong
38 s = ulong 0
39 v = n
40 while gt v 0 bound 20
41 s = add s rem v 10
42 v = div v 10
43 end
44 ret s
45 end
46 note a Smith number is composite and its digit sum equals the digit sums of its prime factors, with
47 note multiplicity; returns 0 for primes and 1
48 fn smith n ulong to bool
49 v = n
50 d = ulong 2
51 f = ulong 0
52 k = ulong 0
53 while le mul d d v bound 200
54 if eq rem v d 0
55 f = add f dsum d
56 k = add k 1
57 v = div v d
58 else
59 d = add d 1
60 end
61 end
62 if gt v 1
63 f = add f dsum v
64 k = add k 1
65 end
66 if lt k 2
67 ret false
68 end
69 ret eq f dsum n
70 end
71 fn main to int
72 sm = new ulong 400 0
73 c = ulong 0
74 for i ulong 9998
75 n = add i 2
76 if smith n
77 put sm c n
78 c = add c 1
79 end
80 end
81 emit cnt
82 emitu c
83 emit nl
84 emit first
85 for i ulong 20
86 emit sp
87 emitu get sm i
88 end
89 emit nl
90 emit last
91 for i ulong 10
92 emit sp
93 emitu get sm add sub c 10 i
94 end
95 emit nl
96 ret 0
97 end
98
99 {{out}}
100
101 Smith numbers below 10000: 376
102 first 20: 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
103 last 10: 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
104