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/tasks/permutations-derangements/permutations-derangements.om # 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 / tasks / permutations-derangements / permutations-derangements.om` 153 lines ยท 2547 bytes Raw Blame History 1 note Rosetta Code task "Permutations/Derangements": https://rosettacode.org/wiki/Permutations/Derangements 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tder "derangements of 0 1 2 3:\n" 8 text thead "n counted !n\n" 9 text tbig "!20 = " 10 global perm ulong 10 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 emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 38 fn emitw n ulong w ulong to void 39 d = new uchar 20 32 40 i = ulong 20 41 v = n 42 while true bound 20 43 i = sub i 1 44 put d i uchar add rem v 10 48 45 v = div v 10 46 if eq v 0 47 break 48 end 49 end 50 if gt w sub 20 i 51 i = sub 20 w 52 end 53 emit part d i sub 20 i 54 end 55 note nextp: the next permutation in lexicographic order, false after the last 56 fn nextp n ulong to bool 57 if lt n 2 58 ret false 59 end 60 i = sub n 1 61 while gt i 0 bound 10 62 if lt get perm sub i 1 get perm i 63 break 64 end 65 i = sub i 1 66 end 67 if eq i 0 68 ret false 69 end 70 j = sub n 1 71 while le get perm j get perm sub i 1 bound 10 72 j = sub j 1 73 end 74 t = get perm sub i 1 75 put perm sub i 1 get perm j 76 put perm j t 77 k = sub n 1 78 while lt i k bound 10 79 t2 = get perm i 80 put perm i get perm k 81 put perm k t2 82 i = add i 1 83 k = sub k 1 84 end 85 ret true 86 end 87 fn deranged n ulong to bool 88 for i ulong n 89 if eq get perm i i 90 ret false 91 end 92 end 93 ret true 94 end 95 note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point; 96 note with show set it also prints them 97 fn count n ulong show bool to ulong 98 for i ulong n 99 put perm i i 100 end 101 c = ulong 0 102 while true bound 362880 103 if deranged n 104 c = add c 1 105 if show 106 for i ulong n 107 if gt i 0 108 emit sp 109 end 110 emitu get perm i 111 end 112 emit nl 113 end 114 end 115 if not nextp n 116 break 117 end 118 end 119 ret c 120 end 121 note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong 122 fn subfact n ulong to ulong 123 a = ulong 1 124 b = ulong 0 125 if eq n 0 126 ret a 127 end 128 for x ulong sub n 1 129 m = add x 1 130 c = mul m add a b 131 a = b 132 b = c 133 end 134 ret b 135 end 136 fn main to int 137 emit tder 138 z = count 4 true 139 emit thead 140 for n ulong 10 141 emitu n 142 emit sp 143 emitw count n false 8 144 emit sp 145 emit sp 146 emitu subfact n 147 emit nl 148 end 149 emit tbig 150 emitu subfact 20 151 emit nl 152 ret 0 153 end