code wiki / (root) / nx_extended_gcd_test.nx

nx_extended_gcd_test.nx source

↩ module page · 29 lines · 1228 B

1import "nx_syscalls.nx" 2import "nx_extended_gcd.nx" 3 4func main() -> nx_int { 5 let buf: *u8 = sys_mmap(16) 6 let x: *nx_int = buf as *nx_int 7 let y: *nx_int = (buf as i64 + 8) as *nx_int 8 9 // gcd(48, 18) = 6; one valid (x, y): (-7, 19)? Actually 48*-1+18*3 = -48+54 = 6. 10 // Whichever Bezout pair the algo picks, a*x+b*y must equal gcd. 11 let g1: nx_int = nx_extended_gcd(48, 18, x, y) 12 if g1 != 6 { return __syscall(93, 1, 0, 0, 0, 0, 0) } 13 if 48 * x[0] + 18 * y[0] != 6 { return __syscall(93, 2, 0, 0, 0, 0, 0) } 14 15 let g2: nx_int = nx_extended_gcd(17, 5, x, y) 16 if g2 != 1 { return __syscall(93, 3, 0, 0, 0, 0, 0) } 17 if 17 * x[0] + 5 * y[0] != 1 { return __syscall(93, 4, 0, 0, 0, 0, 0) } 18 19 let g3: nx_int = nx_extended_gcd(100, 75, x, y) 20 if g3 != 25 { return __syscall(93, 5, 0, 0, 0, 0, 0) } 21 if 100 * x[0] + 75 * y[0] != 25 { return __syscall(93, 6, 0, 0, 0, 0, 0) } 22 23 // gcd(a, 0) = a; Bezout: a*1 + 0*0 = a 24 let g4: nx_int = nx_extended_gcd(7, 0, x, y) 25 if g4 != 7 { return __syscall(93, 7, 0, 0, 0, 0, 0) } 26 if x[0] != 1 { return __syscall(93, 8, 0, 0, 0, 0, 0) } 27 if y[0] != 0 { return __syscall(93, 9, 0, 0, 0, 0, 0) } 28 return 0 29}