omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / fibonacci-sequence.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 | 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 fib n is F(n) with F(0) = 0 and F(1) = 1; F(93) is the largest that fits a ulong |
| 34 | note (the loop computes one term past the one returned; unsigned add wraps, it never traps) |
| 35 | fn fib n ulong to ulong |
| 36 | a = ulong 0 |
| 37 | b = ulong 1 |
| 38 | for i ulong n |
| 39 | t = add a b |
| 40 | a = b |
| 41 | b = t |
| 42 | end |
| 43 | ret a |
| 44 | end |
| 45 | fn main to int |
| 46 | for n ulong 94 |
| 47 | emitu fib n |
| 48 | emit nl |
| 49 | end |
| 50 | ret 0 |
| 51 | end |
| 52 | </syntaxhighlight> |
| 53 | {{out}} |
| 54 | <pre> |
| 55 | 0 |
| 56 | 1 |
| 57 | 1 |
| 58 | 2 |
| 59 | 3 |
| 60 | 5 |
| 61 | 8 |
| 62 | 13 |
| 63 | 21 |
| 64 | 34 |
| 65 | 55 |
| 66 | 89 |
| 67 | 144 |
| 68 | 233 |
| 69 | 377 |
| 70 | ... |
| 71 | 420196140727489673 |
| 72 | 679891637638612258 |
| 73 | 1100087778366101931 |
| 74 | 1779979416004714189 |
| 75 | 2880067194370816120 |
| 76 | 4660046610375530309 |
| 77 | 7540113804746346429 |
| 78 | 12200160415121876738 |
| 79 | </pre> |