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.wiki

120 lines · 1971 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 tcount "permutations: "
10global perm ulong 8
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note nextp steps perm[0..n-1] to the next permutation in lexicographic order; false after the last:
38note find the last ascent i-1 < i, swap perm[i-1] with the last larger element, reverse the tail
39fn nextp n ulong to bool
40i = sub n 1
41while gt i 0 bound 8
42if lt get perm sub i 1 get perm i
43break
44end
45i = sub i 1
46end
47if eq i 0
48ret false
49end
50j = sub n 1
51while le get perm j get perm sub i 1 bound 8
52j = sub j 1
53end
54t = get perm sub i 1
55put perm sub i 1 get perm j
56put perm j t
57k = sub n 1
58while lt i k bound 8
59t2 = get perm i
60put perm i get perm k
61put perm k t2
62i = add i 1
63k = sub k 1
64end
65ret true
66end
67fn show n ulong to void
68for i ulong n
69if gt i 0
70emit sp
71end
72emitu get perm i
73end
74emit nl
75end
76note all 4! orders of 1 2 3 4; 24 permutations bound the loop
77fn main to int
78n = ulong 4
79for i ulong n
80put perm i add i 1
81end
82c = ulong 1
83show n
84while nextp n bound 24
85show n
86c = add c 1
87end
88emit tcount
89emitu c
90emit nl
91ret 0
92end
93</syntaxhighlight>
94{{out}}
95<pre>
961 2 3 4
971 2 4 3
981 3 2 4
991 3 4 2
1001 4 2 3
1011 4 3 2
1022 1 3 4
1032 1 4 3
1042 3 1 4
1052 3 4 1
1062 4 1 3
1072 4 3 1
1083 1 2 4
1093 1 4 2
1103 2 1 4
111...
1123 4 2 1
1134 1 2 3
1144 1 3 2
1154 2 1 3
1164 2 3 1
1174 3 1 2
1184 3 2 1
119permutations: 24
120</pre>