📜  数 N 的第 K 个最大因数

📅  最后修改于: 2022-05-13 01:56:09.747000             🧑  作者: Mango

数 N 的第 K 个最大因数

给定两个正整数NK ,任务是打印 N 的第 K 个最大因子。

方法:这个想法是检查范围 [N, 1] 中的每个数字,并打印完全除以 N 的第 K 个数字。

遍历从N0的循环。现在,对于此循环中的每个数字:

  • 检查它是否除N。
  • 如果N可以被当前数整除,则将K的值减 1。
  • K变为 0 时,这意味着当前数是N 的第 K 个最大因子。
  • 根据以上观察打印答案。

下面是上述方法的实现:

C
// C program for the above approach
 
#include 
 
// Function to print Kth largest
// factor of N
int KthLargestFactor(int N, int K)
{
 
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
int main()
{
    int N = 12, K = 3;
    printf("%d", KthLargestFactor(N, K));
    return 0;
}


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print Kth largest
// factor of N
int KthLargestFactor(int N, int K)
{
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
int main()
{
    int N = 12, K = 3;
    cout << KthLargestFactor(N, K);
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to print Kth largest
    // factor of N
    static int KthLargestFactor(int N, int K)
    {
        // Check for numbers
        // in the range [N, 1]
        for (int i = N; i > 0; i--) {
 
            // Check if i is a factor of N
            if (N % i == 0)
 
                // If Yes, reduce K by 1
                K--;
 
            // If K is 0, it means
            // i is the required
            // Kth factor of N
            if (K == 0) {
                return i;
            }
        }
 
        // When K is more
        // than the factors of N
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 12, K = 3;
        System.out.println(KthLargestFactor(N, K));
    }
}


Python
# Python program for the above approach
 
# Function to print Kth largest
# factor of N
def KthLargestFactor(N, K):
    for i in range(N, 0, -1):
        if N % i == 0:
            K -= 1
        if K == 0:
            return i
    return -1
 
 
# Driver Code
N = 12
K = 3
print(KthLargestFactor(N, K))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print Kth largest
// factor of N
static int KthLargestFactor(int N, int K)
{
 
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
public static void Main()
{
    int N = 12, K = 3;
    Console.Write(KthLargestFactor(N, K));
}
}
 
// This code is contributed by ipg2016107.


Javascript



输出:
4

时间复杂度: O(N)
辅助空间: O(1)