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