interplane.Get Omega
Read in your language

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

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