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 / lucas-lehmer-test.wiki

119 lines · 2564 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 em "M"
9text hdr "Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:"
10note w 0 and w 1 hold the high and low 64 bits of the last 128-bit product
11global w ulong 2
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note mul128 forms a * b exactly from four 32 x 32 -> 64-bit partial products
39fn mul128 a ulong b ulong to void
40al = and a 0xffffffff
41ah = shr a 32
42bl = and b 0xffffffff
43bh = shr b 32
44ll = mul al bl
45lh = mul al bh
46hl = mul ah bl
47hi = add mul ah bh shr lh 32
48hi = add hi shr hl 32
49mid = add shr ll 32 and lh 0xffffffff
50mid = add mid and hl 0xffffffff
51put w 1 add and ll 0xffffffff shl mid 32
52put w 0 add hi shr mid 32
53end
54note sqm is s * s mod m for m = 2^p - 1, 2 < p <= 63,, s < m: the product x = q * 2^p + r folds to q + r
55note x < 2^(2p), so q < 2^p and q + r < 2^64; a second fold and one subtraction finish it
56fn sqm s ulong p ulong m ulong to ulong
57mul128 s s
58q = or shl get w 0 sub 64 p shr get w 1 p
59t = add q and get w 1 m
60t = add and t m shr t p
61if ge t m
62t = sub t m
63end
64ret t
65end
66note p is prime by trial division (p <= 63)
67fn smallp p ulong to bool
68if lt p 2
69ret false
70end
71d = ulong 2
72while le mul d d p bound 8
73if eq rem p d 0
74ret false
75end
76d = add d 1
77end
78ret true
79end
80note Lucas-Lehmer for odd prime p: s = 4, then p - 2 times s = s^2 - 2 mod M; M is prime iff s ends at 0
81fn lltest p ulong to bool
82m = sub shl ulong 1 p 1
83s = ulong 4
84for i ulong sub p 2
85s = sqm s p m
86if ge s 2
87s = sub s 2
88else
89s = sub add s m 2
90end
91end
92ret eq s 0
93end
94fn main to int
95emit hdr
96emit nl
97note M2 = 3 is prime by definition; the test as stated covers odd prime p
98emit em
99emitu 2
100p = ulong 3
101while le p 63 bound 61
102if smallp p
103if lltest p
104emit sp
105emit em
106emitu p
107end
108end
109p = add p 2
110end
111emit nl
112ret 0
113end
114</syntaxhighlight>
115{{out}}
116<pre>
117Mersenne primes 2^p - 1 for p &lt;= 63, by the Lucas-Lehmer test:
118M2 M3 M5 M7 M13 M17 M19 M31 M61
119</pre>