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