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