Count Operations to Obtain Zero

easy math simulation

Problem

Given two non-negative integers num1 and num2, in each operation subtract the smaller from the larger (or do nothing if either is zero). Return the number of operations needed until at least one becomes zero.

Inputnum1 = 2, num2 = 3
Output3
(2,3)→(2,1)→(1,1)→(0,1). 3 operations.

def count_operations(num1, num2):
    ops = 0
    while num1 and num2:
        if num1 >= num2:
            num1 -= num2
        else:
            num2 -= num1
        ops += 1
    return ops
function countOperations(num1, num2) {
  let ops = 0;
  while (num1 && num2) {
    if (num1 >= num2) num1 -= num2;
    else num2 -= num1;
    ops++;
  }
  return ops;
}
class Solution {
    public int countOperations(int num1, int num2) {
        int ops = 0;
        while (num1 != 0 && num2 != 0) {
            if (num1 >= num2) num1 -= num2;
            else num2 -= num1;
            ops++;
        }
        return ops;
    }
}
int countOperations(int num1, int num2) {
    int ops = 0;
    while (num1 && num2) {
        if (num1 >= num2) num1 -= num2;
        else num2 -= num1;
        ops++;
    }
    return ops;
}
Time: O(log(max(a,b))) amortized Space: O(1)