title: K Th Prime Factor Given Number
signature: k_th_prime_factor_given_number <- function(n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      abs(actual - expected) < 1e-06
  }
  
  driver <- function(n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          94L, 0L, 
          -1.0
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          99L, 1L, 
          3.0
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          64L, 3L, 
          2.0
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          27L, 3L, 
          3.0
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          24L, 4L, 
          3.0
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          84L, 6L, 
          -1.0
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          69L, 98L, 
          -1.0
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          69L, 39L, 
          -1.0
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          22L, 60L, 
          -1.0
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          39L, 57L, 
          -1.0
      )))
  
  }
  
  main()
source_py: def k_th_prime_factor_given_number(n, k):
      while ((n % 2) == 0):
          k = (k - 1)
          n = (n / 2)
          if (k == 0):
              return 2
      i = 3
      while (i <= math.sqrt(n)):
          while ((n % i) == 0):
              if (k == 1):
                  return i
              k = (k - 1)
              n = (n / i)
          i = (i + 2)
      if ((n > 2) and (k == 1)):
          return n
      return (- 1)
source_cpp: int k_th_prime_factor_given_number(int n, int k) {
    while ( n % 2 == 0 ) {
      k --;
      n = n / 2;
      if ( k == 0 ) return 2;
    }
    for ( int i = 3;
    i <= sqrt ( n );
    i = i + 2 ) {
      while ( n % i == 0 ) {
        if ( k == 1 ) return i;
        k --;
        n = n / i;
      }
    }
    if ( n > 2 && k == 1 ) return n;
    return - 1;
  }