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 / emirp-primes.wiki

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