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 / josephus-problem.wiki

75 lines · 1558 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 minus "-"
9text tkill "executed in order:"
10text tsurv "survivor: "
11global alive uchar 41
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note 41 prisoners numbered 0..40 stand in a circle; counting from 0, every 3rd one still alive is killed
39note until one is left; each kill walks at most 3 live places, so 3*41 steps per kill bound the walk
40fn main to int
41n = ulong 41
42k = ulong 3
43for i ulong n
44put alive i 1
45end
46p = sub n 1
47emit tkill
48for x ulong sub n 1
49c = ulong 0
50while lt c k bound 123
51p = rem add p 1 n
52if eq get alive p 1
53c = add c 1
54end
55end
56put alive p 0
57emit sp
58emitu p
59end
60emit nl
61emit tsurv
62for i ulong n
63if eq get alive i 1
64emitu i
65end
66end
67emit nl
68ret 0
69end
70</syntaxhighlight>
71{{out}}
72<pre>
73executed in order: 2 5 8 11 14 17 20 23 26 29 32 35 38 0 4 9 13 18 22 27 31 36 40 6 12 19 25 33 39 7 16 28 37 10 24 1 21 3 34 15
74survivor: 30
75</pre>