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