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 / mobius-function / mobius-function.om

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