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
omega-mv / si / rosetta / tasks / mobius-function / mobius-function.om
| 1 | note Rosetta Code task "Möbius function": https://rosettacode.org/wiki/M%C3%B6bius_function |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text hdr "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n" |
| 6 | text blank " " |
| 7 | text tneg " -1" |
| 8 | text tzer " 0" |
| 9 | text tpos " 1" |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note mu(n) by trial division: 0 if some prime divides n twice, else (-1)^(number of prime factors) |
| 22 | note bound: n < 200, so d walks fewer than 15 values with d * d <= n |
| 23 | fn mu n ulong to long |
| 24 | v = n |
| 25 | s = long 1 |
| 26 | d = ulong 2 |
| 27 | while le mul d d v bound 200 |
| 28 | if eq rem v d 0 |
| 29 | v = div v d |
| 30 | if eq rem v d 0 |
| 31 | ret 0 |
| 32 | end |
| 33 | s = sub long 0 s |
| 34 | end |
| 35 | d = add d 1 |
| 36 | end |
| 37 | if gt v 1 |
| 38 | s = sub long 0 s |
| 39 | end |
| 40 | ret s |
| 41 | end |
| 42 | fn main to int |
| 43 | emit hdr |
| 44 | for r ulong 10 |
| 45 | for c ulong 20 |
| 46 | n = add mul r 20 c |
| 47 | if eq n 0 |
| 48 | emit blank |
| 49 | else |
| 50 | m = mu n |
| 51 | if lt m 0 |
| 52 | emit tneg |
| 53 | end |
| 54 | if eq m 0 |
| 55 | emit tzer |
| 56 | end |
| 57 | if gt m 0 |
| 58 | emit tpos |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | emit nl |
| 63 | end |
| 64 | ret 0 |
| 65 | end |