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 / cuban-primes.wiki

165 lines · 3784 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 first "first 200 cuban primes:\n"
9text th "the 100000th cuban prime: "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note cuban primes here are the primes (n+1)^3 - n^3 = 3n^2 + 3n + 1; the 100000th, 1792617147127, is below 2^41
37note mulm is a * b mod m for m < 2^42: b splits into 21-bit halves so each product stays below 2^63
38fn mulm a ulong b ulong m ulong to ulong
39h = mul a shr b 21
40h = rem h m
41h = rem shl h 21 m
42l = rem mul a and b 0x1fffff m
43r = add h l
44if ge r m
45r = sub r m
46end
47ret r
48end
49fn powm b ulong e ulong m ulong to ulong
50r = ulong 1
51x = rem b m
52v = e
53while gt v 0 bound 64
54if eq and v 1 1
55r = mulm r x m
56end
57x = mulm x x m
58v = shr v 1
59end
60ret r
61end
62fn sprp n ulong a ulong to bool
63d = sub n 1
64s = ulong 0
65while eq and d 1 0 bound 64
66d = shr d 1
67s = add s 1
68end
69x = powm a d n
70if or eq x 1 eq x sub n 1
71ret true
72end
73for i ulong sub s 1
74x = mulm x x n
75if eq x sub n 1
76ret true
77end
78end
79ret false
80end
81note trial division by 2..49 first, then Miller-Rabin to bases 2, 3, 5, 7, 11, 13, which is exact below 3.47 * 10^12
82fn isprime n ulong to bool
83p = ulong 2
84while lt p 50 bound 50
85if eq rem n p 0
86ret eq n p
87end
88p = add p 1
89end
90for k ulong 6
91a = ulong 13
92if eq k 0
93a = 2
94end
95if eq k 1
96a = 3
97end
98if eq k 2
99a = 5
100end
101if eq k 3
102a = 7
103end
104if eq k 4
105a = 11
106end
107if not sprp n a
108ret false
109end
110end
111ret true
112end
113note bound: the 100000th cuban prime is reached at n = 773019
114fn main to int
115emit first
116c = ulong 0
117n = ulong 1
118while lt c 100000 bound 999999
119v = add mul mul 3 n add n 1 1
120if isprime v
121c = add c 1
122if le c 200
123emitu v
124if eq rem c 10 0
125emit nl
126else
127emit sp
128end
129end
130if eq c 100000
131emit th
132emitu v
133emit nl
134end
135end
136n = add n 1
137end
138ret 0
139end
140</syntaxhighlight>
141{{out}}
142<pre>
143first 200 cuban primes:
1447 19 37 61 127 271 331 397 547 631
145919 1657 1801 1951 2269 2437 2791 3169 3571 4219
1464447 5167 5419 6211 7057 7351 8269 9241 10267 11719
14712097 13267 13669 16651 19441 19927 22447 23497 24571 25117
14826227 27361 33391 35317 42841 45757 47251 49537 50311 55897
14959221 60919 65269 70687 73477 74419 75367 81181 82171 87211
15088237 89269 92401 96661 102121 103231 104347 110017 112327 114661
151115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
152163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
153234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
154285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
155360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
156436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
157584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
158658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
159772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
160895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
161986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1621125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1631372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
164the 100000th cuban prime: 1792617147127
165</pre>