interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / sum-multiples-of-3-and-5 / sum-multiples-of-3-and-5.om

60 lines · 1274 bytes Raw Blame History
1note Rosetta Code task "Sum multiples of 3 and 5": https://rosettacode.org/wiki/Sum_multiples_of_3_and_5
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text loop "by loop, below 1000: "
6text pre "by formula, below 10^"
7text col ": "
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 the multiples of k below n add up to k * m * (m + 1) / 2 with m = (n - 1) / k; inclusion-exclusion
35note removes the multiples of 15 counted twice. Below 10^9 every product stays under 2^64
36fn sm k ulong n ulong to ulong
37m = div sub n 1 k
38ret div mul mul k m add m 1 2
39end
40fn main to int
41s = ulong 0
42for i ulong 1000
43if or eq rem i 3 0 eq rem i 5 0
44s = add s i
45end
46end
47emit loop
48emitu s
49emit nl
50n = ulong 10
51for e ulong 9
52emit pre
53emitu add e 1
54emit col
55emitu sub add sm 3 n sm 5 n sm 15 n
56emit nl
57n = mul n 10
58end
59ret 0
60end