interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / extensible-prime-generator.wiki

114 lines · 2319 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text first "first 20 primes:"
9text betw "primes between 100 and 150:"
10text cnt "number of primes between 7700 and 8000: "
11text nth "the 10000th prime: "
12note the generator's state: pr holds every prime found so far, st 0 their count, st 1 the last candidate
13note pr has room for 20000 primes; asking for more traps 3 at put, never a wild write
14global pr ulong 20000
15global st ulong 2
16note emit writes every byte of a to stdout; a short write is retried, an error exits 1
17fn emit a view uchar * to void
18o = ulong 0
19while lt o len a bound 4096
20k = write 1 a o sub len a o
21if le k 0
22exit 1
23end
24o = add o ulong k
25end
26end
27note emitu writes n in decimal; 20 digits hold every ulong
28fn emitu n ulong to void
29d = new uchar 20 0
30i = ulong 20
31v = n
32while true bound 20
33i = sub i 1
34put d i uchar add rem v 10 48
35v = div v 10
36if eq v 0
37break
38end
39end
40emit part d i sub 20 i
41end
42note nextp extends the stored list by one prime: the next candidate is tried against stored primes p while p * p <= c
43note bound: a prime gap below 2^32 is far below 999999 candidates; the stored primes checked are at most st 0
44fn nextp to ulong
45c = get st 1
46while true bound 999999
47c = add c 1
48ok = true
49j = ulong 0
50while lt j get st 0 bound 20000
51p = get pr j
52if gt mul p p c
53break
54end
55if eq rem c p 0
56ok = false
57break
58end
59j = add j 1
60end
61if ok
62put pr get st 0 c
63put st 0 add get st 0 1
64put st 1 c
65ret c
66end
67end
68trap 4
69end
70fn main to int
71put st 1 1
72emit first
73for i ulong 20
74emit sp
75emitu nextp
76end
77emit nl
78emit betw
79p = nextp
80while le p 150 bound 1000
81if ge p 100
82emit sp
83emitu p
84end
85p = nextp
86end
87emit nl
88while lt p 7700 bound 10000
89p = nextp
90end
91n = ulong 0
92while le p 8000 bound 10000
93n = add n 1
94p = nextp
95end
96emit cnt
97emitu n
98emit nl
99while lt get st 0 10000 bound 10000
100p = nextp
101end
102emit nth
103emitu get pr 9999
104emit nl
105ret 0
106end
107</syntaxhighlight>
108{{out}}
109<pre>
110first 20 primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
111primes between 100 and 150: 101 103 107 109 113 127 131 137 139 149
112number of primes between 7700 and 8000: 30
113the 10000th prime: 104729
114</pre>