interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / factorial / factorial.om

47 lines · 869 bytes Raw Blame History
1note Rosetta Code task "Factorial": https://rosettacode.org/wiki/Factorial
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text bang "! = "
6note emit writes every byte of a to stdout; a short write is retried, an error exits 1
7fn emit a view uchar * to void
8o = ulong 0
9while lt o len a bound 4096
10k = write 1 a o sub len a o
11if le k 0
12exit 1
13end
14o = add o ulong k
15end
16end
17note emitu writes n in decimal; 20 digits hold every ulong
18fn emitu n ulong to void
19d = new uchar 20 0
20i = ulong 20
21v = n
22while true bound 20
23i = sub i 1
24put d i uchar add rem v 10 48
25v = div v 10
26if eq v 0
27break
28end
29end
30emit part d i sub 20 i
31end
32note recursive; 20! is the largest factorial that fits a ulong
33fn fact n ulong to ulong
34if eq n 0
35ret 1
36end
37ret mul n fact sub n 1
38end
39fn main to int
40for n ulong 21
41emitu n
42emit bang
43emitu fact n
44emit nl
45end
46ret 0
47end