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-derangements.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-derangements.wiki` 181 lines ยท 2949 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 tder "derangements of 0 1 2 3:\n" 10 text thead "n counted !n\n" 11 text tbig "!20 = " 12 global perm ulong 10 13 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 14 fn emit a view uchar * to void 15 o = ulong 0 16 while lt o len a bound 4096 17 k = write 1 a o sub len a o 18 if le k 0 19 exit 1 20 end 21 o = add o ulong k 22 end 23 end 24 note emitu writes n in decimal; 20 digits hold every ulong 25 fn emitu n ulong to void 26 d = new uchar 20 0 27 i = ulong 20 28 v = n 29 while true bound 20 30 i = sub i 1 31 put d i uchar add rem v 10 48 32 v = div v 10 33 if eq v 0 34 break 35 end 36 end 37 emit part d i sub 20 i 38 end 39 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 40 fn emitw n ulong w ulong to void 41 d = new uchar 20 32 42 i = ulong 20 43 v = n 44 while true bound 20 45 i = sub i 1 46 put d i uchar add rem v 10 48 47 v = div v 10 48 if eq v 0 49 break 50 end 51 end 52 if gt w sub 20 i 53 i = sub 20 w 54 end 55 emit part d i sub 20 i 56 end 57 note nextp: the next permutation in lexicographic order, false after the last 58 fn nextp n ulong to bool 59 if lt n 2 60 ret false 61 end 62 i = sub n 1 63 while gt i 0 bound 10 64 if lt get perm sub i 1 get perm i 65 break 66 end 67 i = sub i 1 68 end 69 if eq i 0 70 ret false 71 end 72 j = sub n 1 73 while le get perm j get perm sub i 1 bound 10 74 j = sub j 1 75 end 76 t = get perm sub i 1 77 put perm sub i 1 get perm j 78 put perm j t 79 k = sub n 1 80 while lt i k bound 10 81 t2 = get perm i 82 put perm i get perm k 83 put perm k t2 84 i = add i 1 85 k = sub k 1 86 end 87 ret true 88 end 89 fn deranged n ulong to bool 90 for i ulong n 91 if eq get perm i i 92 ret false 93 end 94 end 95 ret true 96 end 97 note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point; 98 note with show set it also prints them 99 fn count n ulong show bool to ulong 100 for i ulong n 101 put perm i i 102 end 103 c = ulong 0 104 while true bound 362880 105 if deranged n 106 c = add c 1 107 if show 108 for i ulong n 109 if gt i 0 110 emit sp 111 end 112 emitu get perm i 113 end 114 emit nl 115 end 116 end 117 if not nextp n 118 break 119 end 120 end 121 ret c 122 end 123 note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong 124 fn subfact n ulong to ulong 125 a = ulong 1 126 b = ulong 0 127 if eq n 0 128 ret a 129 end 130 for x ulong sub n 1 131 m = add x 1 132 c = mul m add a b 133 a = b 134 b = c 135 end 136 ret b 137 end 138 fn main to int 139 emit tder 140 z = count 4 true 141 emit thead 142 for n ulong 10 143 emitu n 144 emit sp 145 emitw count n false 8 146 emit sp 147 emit sp 148 emitu subfact n 149 emit nl 150 end 151 emit tbig 152 emitu subfact 20 153 emit nl 154 ret 0 155 end 156 157 {{out}} 158

159 derangements of 0 1 2 3:

160 1 0 3 2

161 1 2 3 0

162 1 3 0 2

163 2 0 3 1

164 2 3 0 1

165 2 3 1 0

166 3 0 1 2

167 3 2 0 1

168 3 2 1 0

169 n counted !n

170 0 1 1

171 1 0 0

172 2 1 1

173 3 2 2

174 4 9 9

175 5 44 44

176 6 265 265

177 7 1854 1854

178 8 14833 14833

179 9 133496 133496

180 !20 = `895014631192902121`

181