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 / 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}}
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 cnt "Smith numbers below 10000: "
9text first "first 20:"
10text last "last 10:"
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37fn dsum n ulong to ulong
38s = ulong 0
39v = n
40while gt v 0 bound 20
41s = add s rem v 10
42v = div v 10
43end
44ret s
45end
46note a Smith number is composite and its digit sum equals the digit sums of its prime factors, with
47note multiplicity; returns 0 for primes and 1
48fn smith n ulong to bool
49v = n
50d = ulong 2
51f = ulong 0
52k = ulong 0
53while le mul d d v bound 200
54if eq rem v d 0
55f = add f dsum d
56k = add k 1
57v = div v d
58else
59d = add d 1
60end
61end
62if gt v 1
63f = add f dsum v
64k = add k 1
65end
66if lt k 2
67ret false
68end
69ret eq f dsum n
70end
71fn main to int
72sm = new ulong 400 0
73c = ulong 0
74for i ulong 9998
75n = add i 2
76if smith n
77put sm c n
78c = add c 1
79end
80end
81emit cnt
82emitu c
83emit nl
84emit first
85for i ulong 20
86emit sp
87emitu get sm i
88end
89emit nl
90emit last
91for i ulong 10
92emit sp
93emitu get sm add sub c 10 i
94end
95emit nl
96ret 0
97end
98</syntaxhighlight>
99{{out}}
100<pre>
101Smith numbers below 10000: 376
102first 20: 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
103last 10: 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
104</pre>