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/tasks/cuban-primes/cuban-primes.om # 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 / tasks / cuban-primes / cuban-primes.om` 137 lines ยท 2265 bytes Raw Blame History 1 note Rosetta Code task "Cuban primes": https://rosettacode.org/wiki/Cuban_primes 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text first "first 200 cuban primes:\n" 7 text th "the 100000th cuban prime: " 8 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 9 fn emit a view uchar * to void 10 o = ulong 0 11 while lt o len a bound 4096 12 k = write 1 a o sub len a o 13 if le k 0 14 exit 1 15 end 16 o = add o ulong k 17 end 18 end 19 note emitu writes n in decimal; 20 digits hold every ulong 20 fn emitu n ulong to void 21 d = new uchar 20 0 22 i = ulong 20 23 v = n 24 while true bound 20 25 i = sub i 1 26 put d i uchar add rem v 10 48 27 v = div v 10 28 if eq v 0 29 break 30 end 31 end 32 emit part d i sub 20 i 33 end 34 note cuban primes here are the primes (n+1)^3 - n^3 = 3n^2 + 3n + 1; the 100000th, `1792617147127`, is below 2^41 35 note mulm is a * b mod m for m < 2^42: b splits into 21-bit halves so each product stays below 2^63 36 fn mulm a ulong b ulong m ulong to ulong 37 h = mul a shr b 21 38 h = rem h m 39 h = rem shl h 21 m 40 l = rem mul a and b 0x1fffff m 41 r = add h l 42 if ge r m 43 r = sub r m 44 end 45 ret r 46 end 47 fn powm b ulong e ulong m ulong to ulong 48 r = ulong 1 49 x = rem b m 50 v = e 51 while gt v 0 bound 64 52 if eq and v 1 1 53 r = mulm r x m 54 end 55 x = mulm x x m 56 v = shr v 1 57 end 58 ret r 59 end 60 fn sprp n ulong a ulong to bool 61 d = sub n 1 62 s = ulong 0 63 while eq and d 1 0 bound 64 64 d = shr d 1 65 s = add s 1 66 end 67 x = powm a d n 68 if or eq x 1 eq x sub n 1 69 ret true 70 end 71 for i ulong sub s 1 72 x = mulm x x n 73 if eq x sub n 1 74 ret true 75 end 76 end 77 ret false 78 end 79 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 80 fn isprime n ulong to bool 81 p = ulong 2 82 while lt p 50 bound 50 83 if eq rem n p 0 84 ret eq n p 85 end 86 p = add p 1 87 end 88 for k ulong 6 89 a = ulong 13 90 if eq k 0 91 a = 2 92 end 93 if eq k 1 94 a = 3 95 end 96 if eq k 2 97 a = 5 98 end 99 if eq k 3 100 a = 7 101 end 102 if eq k 4 103 a = 11 104 end 105 if not sprp n a 106 ret false 107 end 108 end 109 ret true 110 end 111 note bound: the 100000th cuban prime is reached at n = 773019 112 fn main to int 113 emit first 114 c = ulong 0 115 n = ulong 1 116 while lt c 100000 bound 999999 117 v = add mul mul 3 n add n 1 1 118 if isprime v 119 c = add c 1 120 if le c 200 121 emitu v 122 if eq rem c 10 0 123 emit nl 124 else 125 emit sp 126 end 127 end 128 if eq c 100000 129 emit th 130 emitu v 131 emit nl 132 end 133 end 134 n = add n 1 135 end 136 ret 0 137 end