title: Steins Algorithm For Finding Gcd 1
signature: class Solution {
  
      public Integer steinsAlgorithmForFindingGcd1(Integer a, Integer b) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(Integer a, Integer b, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(a, b,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(52, 29, 1);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(36, 94, 2);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(12, 6, 6);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(69, 7, 1);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(45, 11, 1);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(7, 51, 1);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(45, 55, 5);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(62, 86, 2);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(96, 63, 3);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(89, 12, 1);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(Integer a, Integer b, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(a, b),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
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 );
  }