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 / gcd.osc

35 lines · 845 bytes Raw Blame History
1// entry: gcd_u64 gcd_i32 lcm_u32
2// expect-run: gcd_u64 48 18 -> 6
3// expect-run: gcd_i32 4294967248 18 -> 6
4// expect-run: lcm_u32 4 6 -> 12
5// expect-run: lcm_u32 0 0 -> trap=DIV0
6// Euclid's algorithm (u64 wraps, i32 checked); lcm traps DIV0 on gcd 0.
7fn gcd_u64(a: u64, b: u64) -> u64 {
8 let mut x: u64 = a;
9 let mut y: u64 = b;
10 while y != 0 bound 200 {
11 let t: u64 = x % y;
12 x = y;
13 y = t;
14 }
15 return x;
16}
17
18fn gcd_i32(a: i32, b: i32) -> i32 {
19 let mut x: i32 = a;
20 let mut y: i32 = b;
21 if x < 0 { x = -x; }
22 if y < 0 { y = -y; }
23 while y != 0 bound 100 {
24 let t: i32 = x % y;
25 x = y;
26 y = t;
27 }
28 return x;
29}
30
31fn lcm_u32(a: u32, b: u32) -> u32 {
32 let g: u64 = gcd_u64(a as u64, b as u64);
33 let l: u64 = (a as u64) / g * (b as u64);
34 return l as u32;
35}