#979. Euclid's algorithm
When the green flag is clicked, this script runs. What number will the cat say?
when green flag clicked set [a v] to (72) set [b v] to (84) repeat until <(b) = (0)> set [t v] to ((a) mod (b)) set [a v] to (b) set [b v] to (t) end say (a)
# When the green flag is clicked
a = 72
b = 84
while not (b == 0):
t = a % b
a = b
b = t
print(a)
// Scratch's mod gives a positive remainder even for negative numbers
function mod(a, b) {
return ((a % b) + b) % b;
}
let a = 0, b = 0, t = 0;
// When the green flag is clicked
a = 72;
b = 84;
while (!(b === 0)) {
t = mod(a, b);
a = b;
b = t;
}
console.log(a);
💡 Not sure? Build it in Scratch and try it out.