omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / prime-decomposition.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text eqs " = " |
| 8 | text times " x " |
| 9 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 10 | fn emit a view uchar * to void |
| 11 | o = ulong 0 |
| 12 | while lt o len a bound 4096 |
| 13 | k = write 1 a o sub len a o |
| 14 | if le k 0 |
| 15 | exit 1 |
| 16 | end |
| 17 | o = add o ulong k |
| 18 | end |
| 19 | end |
| 20 | note emitu writes n in decimal; 20 digits hold every ulong |
| 21 | fn emitu n ulong to void |
| 22 | d = new uchar 20 0 |
| 23 | i = ulong 20 |
| 24 | v = n |
| 25 | while true bound 20 |
| 26 | i = sub i 1 |
| 27 | put d i uchar add rem v 10 48 |
| 28 | v = div v 10 |
| 29 | if eq v 0 |
| 30 | break |
| 31 | end |
| 32 | end |
| 33 | emit part d i sub 20 i |
| 34 | end |
| 35 | note trial division; d stays below 2^32 for these inputs, so mul d d does not wrap |
| 36 | fn factor n ulong to void |
| 37 | emitu n |
| 38 | emit eqs |
| 39 | v = n |
| 40 | d = ulong 2 |
| 41 | first = true |
| 42 | while le mul d d v bound 999999 |
| 43 | if eq rem v d 0 |
| 44 | if not first |
| 45 | emit times |
| 46 | end |
| 47 | first = false |
| 48 | emitu d |
| 49 | v = div v d |
| 50 | else |
| 51 | d = add d 1 |
| 52 | end |
| 53 | end |
| 54 | if gt v 1 |
| 55 | if not first |
| 56 | emit times |
| 57 | end |
| 58 | emitu v |
| 59 | end |
| 60 | emit nl |
| 61 | end |
| 62 | fn main to int |
| 63 | factor 12 |
| 64 | factor 360 |
| 65 | factor 97 |
| 66 | factor 1001 |
| 67 | factor 2147483647 |
| 68 | factor 600851475143 |
| 69 | factor 18446744073709551615 |
| 70 | ret 0 |
| 71 | end |
| 72 | </syntaxhighlight> |
| 73 | {{out}} |
| 74 | <pre> |
| 75 | 12 = 2 x 2 x 3 |
| 76 | 360 = 2 x 2 x 2 x 3 x 3 x 5 |
| 77 | 97 = 97 |
| 78 | 1001 = 7 x 11 x 13 |
| 79 | 2147483647 = 2147483647 |
| 80 | 600851475143 = 71 x 839 x 1471 x 6857 |
| 81 | 18446744073709551615 = 3 x 5 x 17 x 257 x 641 x 65537 x 6700417 |
| 82 | </pre> |