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 / perfect-numbers / perfect-numbers.om

57 lines · 1004 bytes Raw Blame History
1note Rosetta Code task "Perfect numbers": https://rosettacode.org/wiki/Perfect_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5note emit writes every byte of a to stdout; a short write is retried, an error exits 1
6fn emit a view uchar * to void
7o = ulong 0
8while lt o len a bound 4096
9k = write 1 a o sub len a o
10if le k 0
11exit 1
12end
13o = add o ulong k
14end
15end
16note emitu writes n in decimal; 20 digits hold every ulong
17fn emitu n ulong to void
18d = new uchar 20 0
19i = ulong 20
20v = n
21while true bound 20
22i = sub i 1
23put d i uchar add rem v 10 48
24v = div v 10
25if eq v 0
26break
27end
28end
29emit part d i sub 20 i
30end
31note sum of proper divisors, pairing d with n / d up to the square root
32fn perfect n ulong to bool
33if lt n 2
34ret false
35end
36s = ulong 1
37d = ulong 2
38while le mul d d n bound 1000
39if eq rem n d 0
40s = add s d
41if ne mul d d n
42s = add s div n d
43end
44end
45d = add d 1
46end
47ret eq s n
48end
49fn main to int
50for i ulong 10000
51if perfect i
52emitu i
53emit nl
54end
55end
56ret 0
57end