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 / extensible-prime-generator / extensible-prime-generator.om
| 1 | note Rosetta Code task "Extensible prime generator": https://rosettacode.org/wiki/Extensible_prime_generator |
| 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 primes:" |
| 7 | text betw "primes between 100 and 150:" |
| 8 | text cnt "number of primes between 7700 and 8000: " |
| 9 | text nth "the 10000th prime: " |
| 10 | note the generator's state: pr holds every prime found so far, st 0 their count, st 1 the last candidate |
| 11 | note pr has room for 20000 primes; asking for more traps 3 at put, never a wild write |
| 12 | global pr ulong 20000 |
| 13 | global st ulong 2 |
| 14 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 15 | fn emit a view uchar * to void |
| 16 | o = ulong 0 |
| 17 | while lt o len a bound 4096 |
| 18 | k = write 1 a o sub len a o |
| 19 | if le k 0 |
| 20 | exit 1 |
| 21 | end |
| 22 | o = add o ulong k |
| 23 | end |
| 24 | end |
| 25 | note emitu writes n in decimal; 20 digits hold every ulong |
| 26 | fn emitu n ulong to void |
| 27 | d = new uchar 20 0 |
| 28 | i = ulong 20 |
| 29 | v = n |
| 30 | while true bound 20 |
| 31 | i = sub i 1 |
| 32 | put d i uchar add rem v 10 48 |
| 33 | v = div v 10 |
| 34 | if eq v 0 |
| 35 | break |
| 36 | end |
| 37 | end |
| 38 | emit part d i sub 20 i |
| 39 | end |
| 40 | note nextp extends the stored list by one prime: the next candidate is tried against stored primes p while p * p <= c |
| 41 | note bound: a prime gap below 2^32 is far below 999999 candidates; the stored primes checked are at most st 0 |
| 42 | fn nextp to ulong |
| 43 | c = get st 1 |
| 44 | while true bound 999999 |
| 45 | c = add c 1 |
| 46 | ok = true |
| 47 | j = ulong 0 |
| 48 | while lt j get st 0 bound 20000 |
| 49 | p = get pr j |
| 50 | if gt mul p p c |
| 51 | break |
| 52 | end |
| 53 | if eq rem c p 0 |
| 54 | ok = false |
| 55 | break |
| 56 | end |
| 57 | j = add j 1 |
| 58 | end |
| 59 | if ok |
| 60 | put pr get st 0 c |
| 61 | put st 0 add get st 0 1 |
| 62 | put st 1 c |
| 63 | ret c |
| 64 | end |
| 65 | end |
| 66 | trap 4 |
| 67 | end |
| 68 | fn main to int |
| 69 | put st 1 1 |
| 70 | emit first |
| 71 | for i ulong 20 |
| 72 | emit sp |
| 73 | emitu nextp |
| 74 | end |
| 75 | emit nl |
| 76 | emit betw |
| 77 | p = nextp |
| 78 | while le p 150 bound 1000 |
| 79 | if ge p 100 |
| 80 | emit sp |
| 81 | emitu p |
| 82 | end |
| 83 | p = nextp |
| 84 | end |
| 85 | emit nl |
| 86 | while lt p 7700 bound 10000 |
| 87 | p = nextp |
| 88 | end |
| 89 | n = ulong 0 |
| 90 | while le p 8000 bound 10000 |
| 91 | n = add n 1 |
| 92 | p = nextp |
| 93 | end |
| 94 | emit cnt |
| 95 | emitu n |
| 96 | emit nl |
| 97 | while lt get st 0 10000 bound 10000 |
| 98 | p = nextp |
| 99 | end |
| 100 | emit nth |
| 101 | emitu get pr 9999 |
| 102 | emit nl |
| 103 | ret 0 |
| 104 | end |