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/miller-rabin-primality-test.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 / miller-rabin-primality-test.wiki` 171 lines ยท 3126 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 hdr "primes below 100:" 9 text yes ": prime\n" 10 text no ": composite\n" 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 addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b 38 fn addm a ulong b ulong m ulong to ulong 39 if ge a sub m b 40 ret sub a sub m b 41 end 42 ret add a b 43 end 44 note mulm is a * b mod m for any 64-bit m > 0: double and add over the 64 bits of b, high bit first 45 fn mulm a ulong b ulong m ulong to ulong 46 x = rem a m 47 r = ulong 0 48 for i ulong 64 49 r = addm r r m 50 if ne and shr b sub 63 i 1 0 51 r = addm r x m 52 end 53 end 54 ret r 55 end 56 note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings 57 fn powm b ulong e ulong m ulong to ulong 58 r = rem ulong 1 m 59 x = rem b m 60 v = e 61 while gt v 0 bound 64 62 if eq and v 1 1 63 r = mulm r x m 64 end 65 x = mulm x x m 66 v = shr v 1 67 end 68 ret r 69 end 70 note sprp: is odd n > 2 a strong probable prime to base a? n - 1 = d * 2^s with d odd, s at most 63 71 fn sprp n ulong a ulong to bool 72 d = sub n 1 73 s = ulong 0 74 while eq and d 1 0 bound 64 75 d = shr d 1 76 s = add s 1 77 end 78 x = powm a d n 79 if or eq x 1 eq x sub n 1 80 ret true 81 end 82 for i ulong sub s 1 83 x = mulm x x n 84 if eq x sub n 1 85 ret true 86 end 87 end 88 ret false 89 end 90 note the first 12 primes as bases decide every n < 2^64 (3.18 * 10^23 is the first strong pseudoprime to all of them) 91 fn isprime n ulong to bool 92 bs = new ulong 12 0 93 put bs 0 2 94 put bs 1 3 95 put bs 2 5 96 put bs 3 7 97 put bs 4 11 98 put bs 5 13 99 put bs 6 17 100 put bs 7 19 101 put bs 8 23 102 put bs 9 29 103 put bs 10 31 104 put bs 11 37 105 if lt n 2 106 ret false 107 end 108 for i ulong 12 109 a = get bs i 110 if eq n a 111 ret true 112 end 113 if eq rem n a 0 114 ret false 115 end 116 end 117 for i ulong 12 118 if not sprp n get bs i 119 ret false 120 end 121 end 122 ret true 123 end 124 fn tell n ulong to void 125 emitu n 126 if isprime n 127 emit yes 128 else 129 emit no 130 end 131 end 132 fn main to int 133 emit hdr 134 for i ulong 100 135 if isprime i 136 emit sp 137 emitu i 138 end 139 end 140 emit nl 141 tell 561 142 tell 1105 143 tell 1729 144 tell 2047 145 tell `3215031751` 146 tell `2147483647` 147 tell `3825123056546413051` 148 tell `4611686018427387903` 149 tell `2305843009213693951` 150 tell `9223372036854775783` 151 tell `18446744073709551557` 152 tell `18446744073709551615` 153 ret 0 154 end 155 156 {{out}} 157

158 primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

159 561: composite

160 1105: composite

161 1729: composite

162 2047: composite

163 `3215031751`: composite

164 `2147483647`: prime

165 `3825123056546413051`: composite

166 `4611686018427387903`: composite

167 `2305843009213693951`: prime

168 `9223372036854775783`: prime

169 `18446744073709551557`: prime

170 `18446744073709551615`: composite

171