1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00
nu_scripts/modules/recursion/gcd.nu
Stefan Holderbach ba13f5ca60
Fix input-output signatures (#999)
Since 0.101.0 we will finally catch more illegal `def` signatures. As
the grammar for input/output types is rather restricted, this would
error otherwise
2024-12-22 07:58:11 -06:00

15 lines
447 B
Text

# Euclid's algorithm for determining greatest common divisor between 2 positive integers
# Based on this clear explanation from Rutgers: https://sites.math.rutgers.edu/~greenfie/gs2004/euclid.html
# Returns the GCD of its 2 arguments
def gcd [i1: int, i2: int]: nothing -> int {
mut a = $i1; mut b = $i2
if $a < $b { let tmp = $a; $a = $b; $b = $tmp }
let q = $a // $b; let r = $a mod $b
if $r == 0 {
$b
} else {
gcd $b $r
}
}