interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / pascals-triangle / pascals-triangle.om

59 lines · 1154 bytes Raw Blame History
1note Rosetta Code task "Pascal's triangle": https://rosettacode.org/wiki/Pascal's_triangle
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7global row ulong 16
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34note each row comes from the one above in place, right to left: row[j] += row[j-1]; each row is
35note indented so the triangle is centred, two columns per level
36fn pascal n ulong to void
37put row 0 1
38for r ulong n
39for x ulong mul 2 sub sub n r 1
40emit sp
41end
42for j ulong add r 1
43if gt j 0
44emit sp
45end
46emitu get row j
47end
48emit nl
49put row add r 1 1
50for x ulong r
51j = sub r x
52put row j add get row j get row sub j 1
53end
54end
55end
56fn main to int
57pascal 10
58ret 0
59end