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 / count-in-factors / count-in-factors.om
| 1 | note Rosetta Code task "Count in factors": https://rosettacode.org/wiki/Count_in_factors |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text x " x " |
| 6 | text eqs " = " |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note emitu writes n in decimal; 20 digits hold every ulong |
| 19 | fn emitu n ulong to void |
| 20 | d = new uchar 20 0 |
| 21 | i = ulong 20 |
| 22 | v = n |
| 23 | while true bound 20 |
| 24 | i = sub i 1 |
| 25 | put d i uchar add rem v 10 48 |
| 26 | v = div v 10 |
| 27 | if eq v 0 |
| 28 | break |
| 29 | end |
| 30 | end |
| 31 | emit part d i sub 20 i |
| 32 | end |
| 33 | note prime factors in ascending order by trial division; 1 is shown as itself |
| 34 | note bound: each pass divides v or steps d, and d * d <= v; for the inputs here d never passes 6857 |
| 35 | fn tell n ulong to void |
| 36 | emitu n |
| 37 | emit eqs |
| 38 | if eq n 1 |
| 39 | emitu 1 |
| 40 | emit nl |
| 41 | ret |
| 42 | end |
| 43 | v = n |
| 44 | d = ulong 2 |
| 45 | f = ulong 0 |
| 46 | while le mul d d v bound 999999 |
| 47 | if eq rem v d 0 |
| 48 | if gt f 0 |
| 49 | emit x |
| 50 | end |
| 51 | emitu d |
| 52 | f = add f 1 |
| 53 | v = div v d |
| 54 | else |
| 55 | d = add d 1 |
| 56 | end |
| 57 | end |
| 58 | if gt v 1 |
| 59 | if gt f 0 |
| 60 | emit x |
| 61 | end |
| 62 | emitu v |
| 63 | end |
| 64 | emit nl |
| 65 | end |
| 66 | fn main to int |
| 67 | for i ulong 25 |
| 68 | tell add i 1 |
| 69 | end |
| 70 | for i ulong 13 |
| 71 | tell add 2144 i |
| 72 | end |
| 73 | tell 600851475143 |
| 74 | tell 1000000000000 |
| 75 | ret 0 |
| 76 | end |