interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / rosetta / tasks / pernicious-numbers / pernicious-numbers.om

80 lines · 1390 bytes Raw Blame History
1note Rosetta Code task "Pernicious numbers": https://rosettacode.org/wiki/Pernicious_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text first "first 25 pernicious numbers:"
7text range "pernicious numbers from 888888877 to 888888888:"
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34fn popc n ulong to ulong
35c = ulong 0
36v = n
37while gt v 0 bound 64
38c = add c and v 1
39v = shr v 1
40end
41ret c
42end
43note a pernicious number has a prime population count; counts here are at most 30, so trial division is enough
44fn isprime n ulong to bool
45if lt n 2
46ret false
47end
48d = ulong 2
49while le mul d d n bound 8
50if eq rem n d 0
51ret false
52end
53d = add d 1
54end
55ret true
56end
57fn main to int
58emit first
59c = ulong 0
60n = ulong 1
61while lt c 25 bound 100
62if isprime popc n
63emit sp
64emitu n
65c = add c 1
66end
67n = add n 1
68end
69emit nl
70emit range
71for i ulong 12
72n = add 888888877 i
73if isprime popc n
74emit sp
75emitu n
76end
77end
78emit nl
79ret 0
80end