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/cuban-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 / 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}} 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 first "first 200 cuban primes:\n" 9 text th "the 100000th cuban prime: " 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 cuban primes here are the primes (n+1)^3 - n^3 = 3n^2 + 3n + 1; the 100000th, `1792617147127`, is below 2^41 37 note mulm is a * b mod m for m < 2^42: b splits into 21-bit halves so each product stays below 2^63 38 fn mulm a ulong b ulong m ulong to ulong 39 h = mul a shr b 21 40 h = rem h m 41 h = rem shl h 21 m 42 l = rem mul a and b 0x1fffff m 43 r = add h l 44 if ge r m 45 r = sub r m 46 end 47 ret r 48 end 49 fn powm b ulong e ulong m ulong to ulong 50 r = ulong 1 51 x = rem b m 52 v = e 53 while gt v 0 bound 64 54 if eq and v 1 1 55 r = mulm r x m 56 end 57 x = mulm x x m 58 v = shr v 1 59 end 60 ret r 61 end 62 fn sprp n ulong a ulong to bool 63 d = sub n 1 64 s = ulong 0 65 while eq and d 1 0 bound 64 66 d = shr d 1 67 s = add s 1 68 end 69 x = powm a d n 70 if or eq x 1 eq x sub n 1 71 ret true 72 end 73 for i ulong sub s 1 74 x = mulm x x n 75 if eq x sub n 1 76 ret true 77 end 78 end 79 ret false 80 end 81 note 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 82 fn isprime n ulong to bool 83 p = ulong 2 84 while lt p 50 bound 50 85 if eq rem n p 0 86 ret eq n p 87 end 88 p = add p 1 89 end 90 for k ulong 6 91 a = ulong 13 92 if eq k 0 93 a = 2 94 end 95 if eq k 1 96 a = 3 97 end 98 if eq k 2 99 a = 5 100 end 101 if eq k 3 102 a = 7 103 end 104 if eq k 4 105 a = 11 106 end 107 if not sprp n a 108 ret false 109 end 110 end 111 ret true 112 end 113 note bound: the 100000th cuban prime is reached at n = 773019 114 fn main to int 115 emit first 116 c = ulong 0 117 n = ulong 1 118 while lt c 100000 bound 999999 119 v = add mul mul 3 n add n 1 1 120 if isprime v 121 c = add c 1 122 if le c 200 123 emitu v 124 if eq rem c 10 0 125 emit nl 126 else 127 emit sp 128 end 129 end 130 if eq c 100000 131 emit th 132 emitu v 133 emit nl 134 end 135 end 136 n = add n 1 137 end 138 ret 0 139 end 140 141 {{out}} 142

143 first 200 cuban primes:

144 7 19 37 61 127 271 331 397 547 631

145 919 1657 1801 1951 2269 2437 2791 3169 3571 4219

146 4447 5167 5419 6211 7057 7351 8269 9241 10267 11719

147 12097 13267 13669 16651 19441 19927 22447 23497 24571 25117

148 26227 27361 33391 35317 42841 45757 47251 49537 50311 55897

149 59221 60919 65269 70687 73477 74419 75367 81181 82171 87211

150 88237 89269 92401 96661 102121 103231 104347 110017 112327 114661

151 115837 126691 129169 131671 135469 140617 144541 145861 151201 155269

152 163567 169219 170647 176419 180811 189757 200467 202021 213067 231019

153 234361 241117 246247 251431 260191 263737 267307 276337 279991 283669

154 285517 292969 296731 298621 310087 329677 333667 337681 347821 351919

155 360187 368551 372769 374887 377011 383419 387721 398581 407377 423001

156 436627 452797 459817 476407 478801 493291 522919 527941 553411 574219

157 584767 590077 592741 595411 603457 608851 611557 619711 627919 650071

158 658477 666937 689761 692641 698419 707131 733591 742519 760537 769627

159 772669 784897 791047 812761 825301 837937 847477 863497 879667 886177

160 895987 909151 915769 925741 929077 932419 939121 952597 972991 976411

161 986707 990151 997057 `1021417` `1024921` `1035469` `1074607` `1085407` `1110817` `1114471`

162 `1125469` `1155061` `1177507` `1181269` `1215397` `1253887` `1281187` `1285111` `1324681` `1328671`

163 `1372957` `1409731` `1422097` `1426231` `1442827` `1451161` `1480519` `1484737` `1527247` `1570357`

164 the 100000th cuban prime: `1792617147127`

165