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 / square-free-integers / square-free-integers.om

105 lines · 1972 bytes Raw Blame History
1note Rosetta Code task "Square-free integers": https://rosettacode.org/wiki/Square-free_integers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text lo "square-free integers from 1 to 145:"
7text hi "square-free integers from 1000000000000 to 1000000000145:"
8text upto "square-free integers up to "
9text col ": "
10note cp marks composites below 10^6 (a prime sieve); sq n becomes 1 when some p^2 divides n, n <= 10^6;
11note wn marks the window 10^12 .. 10^12 + 145 the same way. Any square factor of a number up to
12note 10^12 + 145 has a prime p <= 10^6 + 1 (p^2 > 10^12 + 145 otherwise), and 10^6 + 1 = 101 * 9901 is composite
13global cp uchar 1000001
14global sq uchar 1000001
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41fn main to int
42wn = new uchar 146 0
43base = ulong 1000000000000
44p = ulong 2
45while le p 1000000 bound 999999
46if eq get cp p 0
47if le mul p p 1000000
48j = mul p p
49while le j 1000000 bound 999999
50put cp j 1
51j = add j p
52end
53end
54q = mul p p
55j = q
56while le j 1000000 bound 999999
57put sq j 1
58j = add j q
59end
60r = rem base q
61j = ulong 0
62if gt r 0
63j = sub q r
64end
65while le j 145 bound 146
66put wn j 1
67j = add j q
68end
69end
70p = add p 1
71end
72emit lo
73for i ulong 145
74if eq get sq add i 1 0
75emit sp
76emitu add i 1
77end
78end
79emit nl
80emit hi
81for i ulong 146
82if eq get wn i 0
83emit sp
84emitu add base i
85end
86end
87emit nl
88c = ulong 0
89lim = ulong 100
90for i ulong 1000000
91n = add i 1
92if eq get sq n 0
93c = add c 1
94end
95if eq n lim
96emit upto
97emitu lim
98emit col
99emitu c
100emit nl
101lim = mul lim 10
102end
103end
104ret 0
105end