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