omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / factorial.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 bang "! = " |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note emitu writes n in decimal; 20 digits hold every ulong |
| 20 | fn emitu n ulong to void |
| 21 | d = new uchar 20 0 |
| 22 | i = ulong 20 |
| 23 | v = n |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put d i uchar add rem v 10 48 |
| 27 | v = div v 10 |
| 28 | if eq v 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | emit part d i sub 20 i |
| 33 | end |
| 34 | note recursive; 20! is the largest factorial that fits a ulong |
| 35 | fn fact n ulong to ulong |
| 36 | if eq n 0 |
| 37 | ret 1 |
| 38 | end |
| 39 | ret mul n fact sub n 1 |
| 40 | end |
| 41 | fn main to int |
| 42 | for n ulong 21 |
| 43 | emitu n |
| 44 | emit bang |
| 45 | emitu fact n |
| 46 | emit nl |
| 47 | end |
| 48 | ret 0 |
| 49 | end |
| 50 | </syntaxhighlight> |
| 51 | {{out}} |
| 52 | <pre> |
| 53 | 0! = 1 |
| 54 | 1! = 1 |
| 55 | 2! = 2 |
| 56 | 3! = 6 |
| 57 | 4! = 24 |
| 58 | 5! = 120 |
| 59 | 6! = 720 |
| 60 | 7! = 5040 |
| 61 | 8! = 40320 |
| 62 | 9! = 362880 |
| 63 | 10! = 3628800 |
| 64 | 11! = 39916800 |
| 65 | 12! = 479001600 |
| 66 | 13! = 6227020800 |
| 67 | 14! = 87178291200 |
| 68 | 15! = 1307674368000 |
| 69 | 16! = 20922789888000 |
| 70 | 17! = 355687428096000 |
| 71 | 18! = 6402373705728000 |
| 72 | 19! = 121645100408832000 |
| 73 | 20! = 2432902008176640000 |
| 74 | </pre> |