gcd

rustemb/extra-functions/gcd

Calculates greatest common (positive) divisor.
gcd
@/gcd
Calculates greatest common (positive) divisor.
IN1number
IN2number (variadic)
gcd
IN1
IN2
OUT
OUTnumber
To use the node in your project you should have the rustemb/extra-functions library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
};

{{ GENERATED_CODE }}

int gcd(int a_, int b_) {
    int a = a_;
	int b = b_;
	if (a < 0) a = -a;
	if (b < 0) b = -b;
	while (b != 0) {
		a %= b;
		if (a == 0) return b;
		b %= a;
	}
	return a;
}

void evaluate(Context ctx) {
    int a = getValue<input_IN1>(ctx);
    int b = getValue<input_IN2>(ctx);
    emitValue<output_OUT>(ctx, gcd(a, b));
}

Tabular tests

IN1IN2OUT
121
341
5105
11171
393
721
1042