interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / josephus-problem / josephus-problem.om

67 lines · 1288 bytes Raw Blame History
1note Rosetta Code task "Josephus problem": https://rosettacode.org/wiki/Josephus_problem
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tkill "executed in order:"
8text tsurv "survivor: "
9global alive uchar 41
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 41 prisoners numbered 0..40 stand in a circle; counting from 0, every 3rd one still alive is killed
37note until one is left; each kill walks at most 3 live places, so 3*41 steps per kill bound the walk
38fn main to int
39n = ulong 41
40k = ulong 3
41for i ulong n
42put alive i 1
43end
44p = sub n 1
45emit tkill
46for x ulong sub n 1
47c = ulong 0
48while lt c k bound 123
49p = rem add p 1 n
50if eq get alive p 1
51c = add c 1
52end
53end
54put alive p 0
55emit sp
56emitu p
57end
58emit nl
59emit tsurv
60for i ulong n
61if eq get alive i 1
62emitu i
63end
64end
65emit nl
66ret 0
67end