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 / permutations-by-swapping.wiki

141 lines · 2911 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 tperm "Perm: ["
10text tsign "] Sign: "
11text tcomma ", "
12global perm ulong 8
13global dir long 8
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41fn emiti n long to void
42if lt n 0
43emit minus
44emitu add bitcast ulong not n 1
45else
46emitu ulong n
47end
48end
49note Steinhaus-Johnson-Trotter with Even's speedup: every element has a direction (all start left);
50note the largest mobile element (one whose neighbour in its direction is smaller) swaps that way, and
51note every larger element turns round; each step is one swap, so the sign alternates
52fn mobile n ulong to ulong
53b = n
54for i ulong n
55j = add long i get dir i
56if and ge j 0 lt j long n
57if gt get perm i get perm ulong j
58if or eq b n gt get perm i get perm b
59b = i
60end
61end
62end
63end
64ret b
65end
66fn show n ulong s long to void
67emit tperm
68for i ulong n
69if gt i 0
70emit tcomma
71end
72emitu get perm i
73end
74emit tsign
75emiti s
76emit nl
77end
78fn sjt n ulong to void
79for i ulong n
80put perm i add i 1
81put dir i -1
82end
83s = long 1
84show n s
85note each pass makes one new permutation and n! of them end the run: 720 = 6! covers n up to 6
86while true bound 720
87m = mobile n
88if eq m n
89break
90end
91v = get perm m
92j = ulong add long m get dir m
93put perm m get perm j
94put perm j v
95t = get dir m
96put dir m get dir j
97put dir j t
98for i ulong n
99if gt get perm i v
100put dir i neg get dir i
101end
102end
103s = neg s
104show n s
105end
106end
107note n = 3 and n = 4: 3! + 4! = 30 lines; 6! = 720 bounds the loop for any n up to 6
108fn main to int
109sjt 3
110emit nl
111sjt 4
112ret 0
113end
114</syntaxhighlight>
115{{out}}
116<pre>
117Perm: [1, 2, 3] Sign: 1
118Perm: [1, 3, 2] Sign: -1
119Perm: [3, 1, 2] Sign: 1
120Perm: [3, 2, 1] Sign: -1
121Perm: [2, 3, 1] Sign: 1
122Perm: [2, 1, 3] Sign: -1
123
124Perm: [1, 2, 3, 4] Sign: 1
125Perm: [1, 2, 4, 3] Sign: -1
126Perm: [1, 4, 2, 3] Sign: 1
127Perm: [4, 1, 2, 3] Sign: -1
128Perm: [4, 1, 3, 2] Sign: 1
129Perm: [1, 4, 3, 2] Sign: -1
130Perm: [1, 3, 4, 2] Sign: 1
131Perm: [1, 3, 2, 4] Sign: -1
132...
133Perm: [2, 3, 1, 4] Sign: 1
134Perm: [2, 3, 4, 1] Sign: -1
135Perm: [2, 4, 3, 1] Sign: 1
136Perm: [4, 2, 3, 1] Sign: -1
137Perm: [4, 2, 1, 3] Sign: 1
138Perm: [2, 4, 1, 3] Sign: -1
139Perm: [2, 1, 4, 3] Sign: 1
140Perm: [2, 1, 3, 4] Sign: -1
141</pre>