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

87 lines · 1578 bytes Raw Blame History
1note Rosetta Code task "Totient function": https://rosettacode.org/wiki/Totient_function
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text phi "phi("
6text eqs ") = "
7text prm ", prime"
8text cnt "primes up to "
9text col ": "
10note ph n is Euler's totient of n for n up to 100000
11global ph ulong 100001
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 emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p)
39note bound: each p touches at most 100000 / p multiples
40fn sieve to void
41for i ulong 100001
42put ph i i
43end
44p = ulong 2
45while le p 100000 bound 100000
46if eq get ph p p
47j = p
48while le j 100000 bound 100000
49put ph j sub get ph j div get ph j p
50j = add j p
51end
52end
53p = add p 1
54end
55end
56note n > 1 is prime exactly when phi(n) = n - 1
57fn main to int
58sieve
59for i ulong 25
60n = add i 1
61emit phi
62emitu n
63emit eqs
64emitu get ph n
65if eq get ph n sub n 1
66emit prm
67end
68emit nl
69end
70lim = ulong 100
71c = ulong 0
72for i ulong 99999
73n = add i 2
74if eq get ph n sub n 1
75c = add c 1
76end
77if eq n lim
78emit cnt
79emitu lim
80emit col
81emitu c
82emit nl
83lim = mul lim 10
84end
85end
86ret 0
87end