interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

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

106 lines · 2042 bytes Raw Blame History
1note Rosetta Code task "Ludic numbers": https://rosettacode.org/wiki/Ludic_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text first "first 25 ludic numbers:"
7text upto "ludic numbers <= 1000: "
8text tfrom "2000th to 2005th ludic numbers:"
9text trip "ludic triplets (x, x+2, x+6) below 250:"
10text lp " ("
11text cm ", "
12text rp ")"
13note the sieve runs on 2..21999; the 2005th ludic number, 21511, is below that limit, and every ludic number
14note found is final because removing every k-th survivor never touches the survivors in front of it
15global ls ulong 22000
16global lu ulong 22000
17global isl uchar 22000
18note emit writes every byte of a to stdout; a short write is retried, an error exits 1
19fn emit a view uchar * to void
20o = ulong 0
21while lt o len a bound 4096
22k = write 1 a o sub len a o
23if le k 0
24exit 1
25end
26o = add o ulong k
27end
28end
29note emitu writes n in decimal; 20 digits hold every ulong
30fn emitu n ulong to void
31d = new uchar 20 0
32i = ulong 20
33v = n
34while true bound 20
35i = sub i 1
36put d i uchar add rem v 10 48
37v = div v 10
38if eq v 0
39break
40end
41end
42emit part d i sub 20 i
43end
44note round: the first survivor k is ludic; it and every k-th survivor after it (positions 0, k, 2k, ...) go
45note bound: each round removes at least one survivor, so at most 21998 rounds over at most 21998 survivors
46fn main to int
47n = ulong 0
48for i ulong 21998
49put ls i add i 2
50n = add n 1
51end
52put lu 0 1
53put isl 1 1
54c = ulong 1
55while gt n 0 bound 22000
56k = get ls 0
57put lu c k
58put isl k 1
59c = add c 1
60m = ulong 0
61for i ulong n
62if ne rem i k 0
63put ls m get ls i
64m = add m 1
65end
66end
67n = m
68end
69emit first
70for i ulong 25
71emit sp
72emitu get lu i
73end
74emit nl
75emit upto
76m = ulong 0
77for i ulong c
78if le get lu i 1000
79m = add m 1
80end
81end
82emitu m
83emit nl
84emit tfrom
85for i ulong 6
86emit sp
87emitu get lu add 1999 i
88end
89emit nl
90emit trip
91for x ulong 244
92if eq get isl x 1
93if and eq get isl add x 2 1 eq get isl add x 6 1
94emit lp
95emitu x
96emit cm
97emitu add x 2
98emit cm
99emitu add x 6
100emit rp
101end
102end
103end
104emit nl
105ret 0
106end