title: Steins Algorithm For Finding Gcd 1
signature: steins_algorithm_for_finding_gcd_1 <- function(a, b) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(a, b, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(a, b), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          52L, 29L, 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          36L, 94L, 
          2L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          12L, 6L, 
          6L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          69L, 7L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          45L, 11L, 
          1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          7L, 51L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          45L, 55L, 
          5L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          62L, 86L, 
          2L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          96L, 63L, 
          3L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          89L, 12L, 
          1L
      )))
  
  }
  
  main()
source_py: def steins_algorithm_for_finding_gcd_1(a, b):
      if (a == b):
          return a
      if (a == 0):
          return b
      if (b == 0):
          return a
      if (((~ a) & 1) == 1):
          if ((b & 1) == 1):
              return steins_algorithm_for_finding_gcd_1((a >> 1), b)
          else:
              return (steins_algorithm_for_finding_gcd_1((a >> 1), (b >> 1)) << 1)
      if (((~ b) & 1) == 1):
          return steins_algorithm_for_finding_gcd_1(a, (b >> 1))
      if (a > b):
          return steins_algorithm_for_finding_gcd_1(((a - b) >> 1), b)
      return steins_algorithm_for_finding_gcd_1(((b - a) >> 1), a)
source_cpp: int steins_algorithm_for_finding_gcd_1(int a, int b) {
    if ( a == b ) return a;
    if ( a == 0 ) return b;
    if ( b == 0 ) return a;
    if ( ~ a & 1 ) {
      if ( b & 1 ) return steins_algorithm_for_finding_gcd_1 ( a >> 1, b );
      else return steins_algorithm_for_finding_gcd_1 ( a >> 1, b >> 1 ) << 1;
    }
    if ( ~ b & 1 ) return steins_algorithm_for_finding_gcd_1 ( a, b >> 1 );
    if ( a > b ) return steins_algorithm_for_finding_gcd_1 ( ( a - b ) >> 1, b );
    return steins_algorithm_for_finding_gcd_1 ( ( b - a ) >> 1, a );
  }