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 / tests / compiler / progs / factorial.osc

24 lines · 599 bytes Raw Blame History
1// entry: fact_i32 fact_u16
2// expect-run: fact_i32 12 -> 479001600
3// expect-run: fact_i32 13 -> trap=OVERFLOW
4// expect-run: fact_i32 4294967295 -> 4294967295
5// expect-run: fact_u16 5 -> 120
6// checked factorial in i32 (OVERFLOW from 13!), wrapping in u16.
7fn fact_i32(n: i32) -> i32 {
8 if n < 0 { return -1; }
9 let mut r: i32 = 1;
10 let mut k: i32 = 2;
11 while k <= n bound 40 {
12 r = r * k;
13 k = k + 1;
14 }
15 return r;
16}
17
18fn fact_u16(n: u8) -> u16 {
19 let mut r: u16 = 1;
20 for k in 2 .. 30 {
21 if k as u8 <= n { r = r * (k as u16); }
22 }
23 return r;
24}