interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / mobius-function.wiki

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