omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / mobius-function.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text hdr "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n" |
| 8 | text blank " " |
| 9 | text tneg " -1" |
| 10 | text tzer " 0" |
| 11 | text tpos " 1" |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note mu(n) by trial division: 0 if some prime divides n twice, else (-1)^(number of prime factors) |
| 24 | note bound: n < 200, so d walks fewer than 15 values with d * d <= n |
| 25 | fn mu n ulong to long |
| 26 | v = n |
| 27 | s = long 1 |
| 28 | d = ulong 2 |
| 29 | while le mul d d v bound 200 |
| 30 | if eq rem v d 0 |
| 31 | v = div v d |
| 32 | if eq rem v d 0 |
| 33 | ret 0 |
| 34 | end |
| 35 | s = sub long 0 s |
| 36 | end |
| 37 | d = add d 1 |
| 38 | end |
| 39 | if gt v 1 |
| 40 | s = sub long 0 s |
| 41 | end |
| 42 | ret s |
| 43 | end |
| 44 | fn main to int |
| 45 | emit hdr |
| 46 | for r ulong 10 |
| 47 | for c ulong 20 |
| 48 | n = add mul r 20 c |
| 49 | if eq n 0 |
| 50 | emit blank |
| 51 | else |
| 52 | m = mu n |
| 53 | if lt m 0 |
| 54 | emit tneg |
| 55 | end |
| 56 | if eq m 0 |
| 57 | emit tzer |
| 58 | end |
| 59 | if gt m 0 |
| 60 | emit tpos |
| 61 | end |
| 62 | end |
| 63 | end |
| 64 | emit nl |
| 65 | end |
| 66 | ret 0 |
| 67 | end |
| 68 | </syntaxhighlight> |
| 69 | {{out}} |
| 70 | <pre> |
| 71 | first 199 terms of the Moebius sequence (the first cell, n = 0, is blank): |
| 72 | 1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1 0 -1 0 -1 |
| 73 | 0 1 1 -1 0 0 1 0 0 -1 -1 -1 0 1 1 1 0 -1 1 1 |
| 74 | 0 -1 -1 -1 0 0 1 -1 0 0 0 1 0 -1 0 1 0 1 1 -1 |
| 75 | 0 -1 1 0 0 1 -1 -1 0 1 -1 -1 0 -1 1 0 0 1 -1 -1 |
| 76 | 0 0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0 |
| 77 | 0 -1 -1 -1 0 -1 1 -1 0 -1 -1 1 0 -1 -1 1 0 0 1 1 |
| 78 | 0 0 1 1 0 0 0 -1 0 1 -1 -1 0 1 1 0 0 -1 -1 -1 |
| 79 | 0 1 1 1 0 1 1 0 0 -1 0 -1 0 0 -1 1 0 -1 1 1 |
| 80 | 0 1 0 -1 0 -1 1 -1 0 0 -1 0 0 -1 -1 0 0 1 1 -1 |
| 81 | 0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1 |
| 82 | </pre> |