omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / pascals-triangle.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 sp " " |
| 8 | text minus "-" |
| 9 | global row ulong 16 |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note each row comes from the one above in place, right to left: row[j] += row[j-1]; each row is |
| 37 | note indented so the triangle is centred, two columns per level |
| 38 | fn pascal n ulong to void |
| 39 | put row 0 1 |
| 40 | for r ulong n |
| 41 | for x ulong mul 2 sub sub n r 1 |
| 42 | emit sp |
| 43 | end |
| 44 | for j ulong add r 1 |
| 45 | if gt j 0 |
| 46 | emit sp |
| 47 | end |
| 48 | emitu get row j |
| 49 | end |
| 50 | emit nl |
| 51 | put row add r 1 1 |
| 52 | for x ulong r |
| 53 | j = sub r x |
| 54 | put row j add get row j get row sub j 1 |
| 55 | end |
| 56 | end |
| 57 | end |
| 58 | fn main to int |
| 59 | pascal 10 |
| 60 | ret 0 |
| 61 | end |
| 62 | </syntaxhighlight> |
| 63 | {{out}} |
| 64 | <pre> |
| 65 | 1 |
| 66 | 1 1 |
| 67 | 1 2 1 |
| 68 | 1 3 3 1 |
| 69 | 1 4 6 4 1 |
| 70 | 1 5 10 10 5 1 |
| 71 | 1 6 15 20 15 6 1 |
| 72 | 1 7 21 35 35 21 7 1 |
| 73 | 1 8 28 56 70 56 28 8 1 |
| 74 | 1 9 36 84 126 126 84 36 9 1 |
| 75 | </pre> |