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