Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/permutations.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / permutations.wiki`
120 lines ยท 1971 bytes Raw Blame History
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 (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tcount "permutations: "
10 global perm ulong 8
11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12 fn emit a view uchar * to void
13 o = ulong 0
14 while lt o len a bound 4096
15 k = write 1 a o sub len a o
16 if le k 0
17 exit 1
18 end
19 o = add o ulong k
20 end
21 end
22 note emitu writes n in decimal; 20 digits hold every ulong
23 fn emitu n ulong to void
24 d = new uchar 20 0
25 i = ulong 20
26 v = n
27 while true bound 20
28 i = sub i 1
29 put d i uchar add rem v 10 48
30 v = div v 10
31 if eq v 0
32 break
33 end
34 end
35 emit part d i sub 20 i
36 end
37 note nextp steps perm[0..n-1] to the next permutation in lexicographic order; false after the last:
38 note find the last ascent i-1 < i, swap perm[i-1] with the last larger element, reverse the tail
39 fn nextp n ulong to bool
40 i = sub n 1
41 while gt i 0 bound 8
42 if lt get perm sub i 1 get perm i
43 break
44 end
45 i = sub i 1
46 end
47 if eq i 0
48 ret false
49 end
50 j = sub n 1
51 while le get perm j get perm sub i 1 bound 8
52 j = sub j 1
53 end
54 t = get perm sub i 1
55 put perm sub i 1 get perm j
56 put perm j t
57 k = sub n 1
58 while lt i k bound 8
59 t2 = get perm i
60 put perm i get perm k
61 put perm k t2
62 i = add i 1
63 k = sub k 1
64 end
65 ret true
66 end
67 fn show n ulong to void
68 for i ulong n
69 if gt i 0
70 emit sp
71 end
72 emitu get perm i
73 end
74 emit nl
75 end
76 note all 4! orders of 1 2 3 4; 24 permutations bound the loop
77 fn main to int
78 n = ulong 4
79 for i ulong n
80 put perm i add i 1
81 end
82 c = ulong 1
83 show n
84 while nextp n bound 24
85 show n
86 c = add c 1
87 end
88 emit tcount
89 emitu c
90 emit nl
91 ret 0
92 end
93
94 {{out}}
95
96 1 2 3 4
97 1 2 4 3
98 1 3 2 4
99 1 3 4 2
100 1 4 2 3
101 1 4 3 2
102 2 1 3 4
103 2 1 4 3
104 2 3 1 4
105 2 3 4 1
106 2 4 1 3
107 2 4 3 1
108 3 1 2 4
109 3 1 4 2
110 3 2 1 4
111 ...
112 3 4 2 1
113 4 1 2 3
114 4 1 3 2
115 4 2 1 3
116 4 2 3 1
117 4 3 1 2
118 4 3 2 1
119 permutations: 24
120