interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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