title: K Th Prime Factor Given Number
signature: float kThPrimeFactorGivenNumber(int n, int k) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(float actual, float expected){
      return abs(actual - expected) < 1e-06;
  }
  
  string driver(int n, int k, float expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n, k), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(94, 0, -1.0); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(99, 1, 3.0); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(64, 3, 2.0); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(27, 3, 3.0); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(24, 4, 3.0); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver(84, 6, -1.0); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver(69, 98, -1.0); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver(69, 39, -1.0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver(22, 60, -1.0); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver(39, 57, -1.0); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }