omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / emirp-primes.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text first "first 20 emirps:" |
| 9 | text betw "emirps between 7700 and 8000:" |
| 10 | text nth "the 10000th emirp: " |
| 11 | note cp i is 1 when i is composite; the sieve covers every number below 10^6, which holds each 6-digit reversal |
| 12 | global cp uchar 1000000 |
| 13 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 14 | fn emit a view uchar * to void |
| 15 | o = ulong 0 |
| 16 | while lt o len a bound 4096 |
| 17 | k = write 1 a o sub len a o |
| 18 | if le k 0 |
| 19 | exit 1 |
| 20 | end |
| 21 | o = add o ulong k |
| 22 | end |
| 23 | end |
| 24 | note emitu writes n in decimal; 20 digits hold every ulong |
| 25 | fn emitu n ulong to void |
| 26 | d = new uchar 20 0 |
| 27 | i = ulong 20 |
| 28 | v = n |
| 29 | while true bound 20 |
| 30 | i = sub i 1 |
| 31 | put d i uchar add rem v 10 48 |
| 32 | v = div v 10 |
| 33 | if eq v 0 |
| 34 | break |
| 35 | end |
| 36 | end |
| 37 | emit part d i sub 20 i |
| 38 | end |
| 39 | note bound: i * i < 10^6 needs i < 1000; the multiples of one i below 10^6 are fewer than 500000 |
| 40 | fn sieve to void |
| 41 | put cp 0 1 |
| 42 | put cp 1 1 |
| 43 | i = ulong 2 |
| 44 | while lt mul i i 1000000 bound 1000 |
| 45 | if eq get cp i 0 |
| 46 | j = mul i i |
| 47 | while lt j 1000000 bound 999999 |
| 48 | put cp j 1 |
| 49 | j = add j i |
| 50 | end |
| 51 | end |
| 52 | i = add i 1 |
| 53 | end |
| 54 | end |
| 55 | note rev reverses the decimal digits of n; a ulong has at most 20 digits |
| 56 | fn rev n ulong to ulong |
| 57 | r = ulong 0 |
| 58 | v = n |
| 59 | while gt v 0 bound 20 |
| 60 | r = add mul r 10 rem v 10 |
| 61 | v = div v 10 |
| 62 | end |
| 63 | ret r |
| 64 | end |
| 65 | note an emirp is a prime whose reversal is a different prime; n stays below 10^6 here |
| 66 | fn emirp n ulong to bool |
| 67 | if ne get cp n 0 |
| 68 | ret false |
| 69 | end |
| 70 | r = rev n |
| 71 | if eq r n |
| 72 | ret false |
| 73 | end |
| 74 | ret eq get cp r 0 |
| 75 | end |
| 76 | fn main to int |
| 77 | sieve |
| 78 | emit first |
| 79 | n = ulong 0 |
| 80 | k = ulong 0 |
| 81 | while lt k 20 bound 1000 |
| 82 | n = add n 1 |
| 83 | if emirp n |
| 84 | k = add k 1 |
| 85 | emit sp |
| 86 | emitu n |
| 87 | end |
| 88 | end |
| 89 | emit nl |
| 90 | emit betw |
| 91 | n = ulong 7700 |
| 92 | while le n 8000 bound 1000 |
| 93 | if emirp n |
| 94 | emit sp |
| 95 | emitu n |
| 96 | end |
| 97 | n = add n 1 |
| 98 | end |
| 99 | emit nl |
| 100 | n = ulong 0 |
| 101 | k = ulong 0 |
| 102 | while lt k 10000 bound 999999 |
| 103 | n = add n 1 |
| 104 | if emirp n |
| 105 | k = add k 1 |
| 106 | end |
| 107 | end |
| 108 | emit nth |
| 109 | emitu n |
| 110 | emit nl |
| 111 | ret 0 |
| 112 | end |
| 113 | </syntaxhighlight> |
| 114 | {{out}} |
| 115 | <pre> |
| 116 | first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389 |
| 117 | emirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963 |
| 118 | the 10000th emirp: 948349 |
| 119 | </pre> |