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 / harshad-or-niven-series.wiki

74 lines · 1509 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 first "first 20 Harshad numbers:"
9text above "first Harshad number above 1000: "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note n is a Harshad number when its digit sum divides it
37fn harshad n ulong to bool
38s = ulong 0
39v = n
40while gt v 0 bound 20
41s = add s rem v 10
42v = div v 10
43end
44ret eq rem n s 0
45end
46note bound: every multiple of 10 up to 100 is Harshad, so neither search passes 1000 candidates
47fn main to int
48emit first
49n = ulong 0
50k = ulong 0
51while lt k 20 bound 1000
52n = add n 1
53if harshad n
54emit sp
55emitu n
56k = add k 1
57end
58end
59emit nl
60n = ulong 1001
61while not harshad n bound 1000
62n = add n 1
63end
64emit above
65emitu n
66emit nl
67ret 0
68end
69</syntaxhighlight>
70{{out}}
71<pre>
72first 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
73first Harshad number above 1000: 1002
74</pre>