interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / smith-numbers / smith-numbers.om

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