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