interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / harshad-or-niven-series / harshad-or-niven-series.om

66 lines · 1280 bytes Raw Blame History
1note Rosetta Code task "Harshad or Niven series": https://rosettacode.org/wiki/Harshad_or_Niven_series
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text first "first 20 Harshad numbers:"
7text above "first Harshad number above 1000: "
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34note n is a Harshad number when its digit sum divides it
35fn harshad n ulong to bool
36s = ulong 0
37v = n
38while gt v 0 bound 20
39s = add s rem v 10
40v = div v 10
41end
42ret eq rem n s 0
43end
44note bound: every multiple of 10 up to 100 is Harshad, so neither search passes 1000 candidates
45fn main to int
46emit first
47n = ulong 0
48k = ulong 0
49while lt k 20 bound 1000
50n = add n 1
51if harshad n
52emit sp
53emitu n
54k = add k 1
55end
56end
57emit nl
58n = ulong 1001
59while not harshad n bound 1000
60n = add n 1
61end
62emit above
63emitu n
64emit nl
65ret 0
66end