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-mv/blob/main/si/rosetta/tasks/miller-rabin-primality-test/miller-rabin-primality-test.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / miller-rabin-primality-test / miller-rabin-primality-test.om` 152 lines ยท 2652 bytes Raw Blame History 1 note Rosetta Code task "Miller-Rabin primality test": https://rosettacode.org/wiki/Miller-Rabin_primality_test 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text hdr "primes below 100:" 7 text yes ": prime\n" 8 text no ": composite\n" 9 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 10 fn emit a view uchar * to void 11 o = ulong 0 12 while lt o len a bound 4096 13 k = write 1 a o sub len a o 14 if le k 0 15 exit 1 16 end 17 o = add o ulong k 18 end 19 end 20 note emitu writes n in decimal; 20 digits hold every ulong 21 fn emitu n ulong to void 22 d = new uchar 20 0 23 i = ulong 20 24 v = n 25 while true bound 20 26 i = sub i 1 27 put d i uchar add rem v 10 48 28 v = div v 10 29 if eq v 0 30 break 31 end 32 end 33 emit part d i sub 20 i 34 end 35 note addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b 36 fn addm a ulong b ulong m ulong to ulong 37 if ge a sub m b 38 ret sub a sub m b 39 end 40 ret add a b 41 end 42 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 43 fn mulm a ulong b ulong m ulong to ulong 44 x = rem a m 45 r = ulong 0 46 for i ulong 64 47 r = addm r r m 48 if ne and shr b sub 63 i 1 0 49 r = addm r x m 50 end 51 end 52 ret r 53 end 54 note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings 55 fn powm b ulong e ulong m ulong to ulong 56 r = rem ulong 1 m 57 x = rem b m 58 v = e 59 while gt v 0 bound 64 60 if eq and v 1 1 61 r = mulm r x m 62 end 63 x = mulm x x m 64 v = shr v 1 65 end 66 ret r 67 end 68 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 69 fn sprp n ulong a ulong to bool 70 d = sub n 1 71 s = ulong 0 72 while eq and d 1 0 bound 64 73 d = shr d 1 74 s = add s 1 75 end 76 x = powm a d n 77 if or eq x 1 eq x sub n 1 78 ret true 79 end 80 for i ulong sub s 1 81 x = mulm x x n 82 if eq x sub n 1 83 ret true 84 end 85 end 86 ret false 87 end 88 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) 89 fn isprime n ulong to bool 90 bs = new ulong 12 0 91 put bs 0 2 92 put bs 1 3 93 put bs 2 5 94 put bs 3 7 95 put bs 4 11 96 put bs 5 13 97 put bs 6 17 98 put bs 7 19 99 put bs 8 23 100 put bs 9 29 101 put bs 10 31 102 put bs 11 37 103 if lt n 2 104 ret false 105 end 106 for i ulong 12 107 a = get bs i 108 if eq n a 109 ret true 110 end 111 if eq rem n a 0 112 ret false 113 end 114 end 115 for i ulong 12 116 if not sprp n get bs i 117 ret false 118 end 119 end 120 ret true 121 end 122 fn tell n ulong to void 123 emitu n 124 if isprime n 125 emit yes 126 else 127 emit no 128 end 129 end 130 fn main to int 131 emit hdr 132 for i ulong 100 133 if isprime i 134 emit sp 135 emitu i 136 end 137 end 138 emit nl 139 tell 561 140 tell 1105 141 tell 1729 142 tell 2047 143 tell `3215031751` 144 tell `2147483647` 145 tell `3825123056546413051` 146 tell `4611686018427387903` 147 tell `2305843009213693951` 148 tell `9223372036854775783` 149 tell `18446744073709551557` 150 tell `18446744073709551615` 151 ret 0 152 end