title: Steins Algorithm For Finding Gcd 1
signature: func steinsAlgorithmForFindingGcd1(a int, b int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(a int, b int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(a, b),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver(52, 
  	29, 1)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver(36, 
  	94, 2)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver(12, 
  	6, 6)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver(69, 
  	7, 1)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver(45, 
  	11, 1)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver(7, 
  	51, 1)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver(45, 
  	55, 5)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver(62, 
  	86, 2)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver(96, 
  	63, 3)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver(89, 
  	12, 1)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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 );
  }