gcd

std.elementwise.gcd · Level L4

The greatest common divisor of each pair of positive integers, by Euclid's algorithm: a While loop of euclid_step until every remainder is zero.

gcd(a, b) = gcd(b, a mod b), gcd(a, 0) = a

Signature

gcd(a: i64[n], b: i64[n]) → i64[n]

Structure

The function as NOVA stores it: one box per input, operation and output, and arrows that carry values. A double border marks another library function this one runs — called once, or by Scan once per element; select it to open that function.

ai64[n]bi64[n]Reshapea_rReshapeb_rConcatstateWhile·euclid_stepfinalSliceg_rReshapeggi64[n]
  • input
  • operation
  • constant
  • call
  • output

Verification

  • Signature proven by NOVA’s shape solver, for every size.
  • Equal to the reference math.gcd(a, b) in exact rational arithmetic, on all 40 test cases.
  • All 174 int64 results are exact: the error is zero.
  • Interpreter and NumPy backend return bit-identical results.
Accuracy in detail
correctly rounded (the int64 nearest the exact value)
100%
bit-equal to the NumPy formula in int64
100%
largest error, in units in the last place
0

Identity

Calls
Called by
—
sha256:e9642b65471a722324006fdaa06e287ea6f19f48e311db79f31f354b51ef7e45

The semantic hash of the graph. It changes when the program changes, and never when only its documentation does.