title: K Th Prime Factor Given Number
signature: function k_th_prime_factor_given_number(n, k)
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;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return math.abs(actual - expected) < 1e-06
  end
  
  function driver(n, k, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(n, k) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver(94, 0, -1.0)
      print(string.format("TEST-0...%s",result))
  
      result = driver(99, 1, 3.0)
      print(string.format("TEST-1...%s",result))
  
      result = driver(64, 3, 2.0)
      print(string.format("TEST-2...%s",result))
  
      result = driver(27, 3, 3.0)
      print(string.format("TEST-3...%s",result))
  
      result = driver(24, 4, 3.0)
      print(string.format("TEST-4...%s",result))
  
      result = driver(84, 6, -1.0)
      print(string.format("TEST-5...%s",result))
  
      result = driver(69, 98, -1.0)
      print(string.format("TEST-6...%s",result))
  
      result = driver(69, 39, -1.0)
      print(string.format("TEST-7...%s",result))
  
      result = driver(22, 60, -1.0)
      print(string.format("TEST-8...%s",result))
  
      result = driver(39, 57, -1.0)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": -1, "inputs": {"n": 94, "k": 0}}, {"idx": 1, "outputs": 3, "inputs": {"n": 99, "k": 1}}, {"idx": 2, "outputs": 2, "inputs": {"n": 64, "k": 3}}, {"idx": 3, "outputs": 3, "inputs": {"n": 27, "k": 3}}, {"idx": 4, "outputs": 3.0, "inputs": {"n": 24, "k": 4}}, {"idx": 5, "outputs": -1, "inputs": {"n": 84, "k": 6}}, {"idx": 6, "outputs": -1, "inputs": {"n": 69, "k": 98}}, {"idx": 7, "outputs": -1, "inputs": {"n": 69, "k": 39}}, {"idx": 8, "outputs": -1, "inputs": {"n": 22, "k": 60}}, {"idx": 9, "outputs": -1, "inputs": {"n": 39, "k": 57}}]
question_info.extension: lua
question_info.entry_fn_name: k_th_prime_factor_given_number
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__