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