omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / si / rosetta / tasks / emirp-primes / emirp-primes.om
| 1 | note Rosetta Code task "Emirp primes": https://rosettacode.org/wiki/Emirp_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 20 emirps:" |
| 7 | text betw "emirps between 7700 and 8000:" |
| 8 | text nth "the 10000th emirp: " |
| 9 | note cp i is 1 when i is composite; the sieve covers every number below 10^6, which holds each 6-digit reversal |
| 10 | global cp uchar 1000000 |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note bound: i * i < 10^6 needs i < 1000; the multiples of one i below 10^6 are fewer than 500000 |
| 38 | fn sieve to void |
| 39 | put cp 0 1 |
| 40 | put cp 1 1 |
| 41 | i = ulong 2 |
| 42 | while lt mul i i 1000000 bound 1000 |
| 43 | if eq get cp i 0 |
| 44 | j = mul i i |
| 45 | while lt j 1000000 bound 999999 |
| 46 | put cp j 1 |
| 47 | j = add j i |
| 48 | end |
| 49 | end |
| 50 | i = add i 1 |
| 51 | end |
| 52 | end |
| 53 | note rev reverses the decimal digits of n; a ulong has at most 20 digits |
| 54 | fn rev n ulong to ulong |
| 55 | r = ulong 0 |
| 56 | v = n |
| 57 | while gt v 0 bound 20 |
| 58 | r = add mul r 10 rem v 10 |
| 59 | v = div v 10 |
| 60 | end |
| 61 | ret r |
| 62 | end |
| 63 | note an emirp is a prime whose reversal is a different prime; n stays below 10^6 here |
| 64 | fn emirp n ulong to bool |
| 65 | if ne get cp n 0 |
| 66 | ret false |
| 67 | end |
| 68 | r = rev n |
| 69 | if eq r n |
| 70 | ret false |
| 71 | end |
| 72 | ret eq get cp r 0 |
| 73 | end |
| 74 | fn main to int |
| 75 | sieve |
| 76 | emit first |
| 77 | n = ulong 0 |
| 78 | k = ulong 0 |
| 79 | while lt k 20 bound 1000 |
| 80 | n = add n 1 |
| 81 | if emirp n |
| 82 | k = add k 1 |
| 83 | emit sp |
| 84 | emitu n |
| 85 | end |
| 86 | end |
| 87 | emit nl |
| 88 | emit betw |
| 89 | n = ulong 7700 |
| 90 | while le n 8000 bound 1000 |
| 91 | if emirp n |
| 92 | emit sp |
| 93 | emitu n |
| 94 | end |
| 95 | n = add n 1 |
| 96 | end |
| 97 | emit nl |
| 98 | n = ulong 0 |
| 99 | k = ulong 0 |
| 100 | while lt k 10000 bound 999999 |
| 101 | n = add n 1 |
| 102 | if emirp n |
| 103 | k = add k 1 |
| 104 | end |
| 105 | end |
| 106 | emit nth |
| 107 | emitu n |
| 108 | emit nl |
| 109 | ret 0 |
| 110 | end |