📜  找到恰好有 4 个除数的第 N 个最小数

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

找到恰好有 4 个除数的第 N 个最小数

给定一个正整数N ,任务是找到序列中恰好有4 个除数的第N个最小数。

例子:

方法:这个问题可以通过观察数i的质因数分解为p1 a1 * p2 a2 * p3 a3 ...pk ak来解决,那么i的除数为(a1+1)(a2+1)...( ak+1)。因此,对于i恰好有4 个除数,它应该等于两个不同素数的乘积 或某个素数的3次幂。请按照以下步骤解决问题:

  • 初始化数组divs[]vis[]以存储任意数的除数,并分别检查是否考虑给定数。
  • 将变量cnt初始化为0以存储恰好具有4 个除数的元素数。
  • 现在,使用 Eratosthenes 算法的筛子。
  • cnt小于n 时迭代,使用从2开始的变量i
    • 如果i是素数:
      • 使用增量为i的变量j[2*i, 1000000]范围内迭代:
        • 如果已经考虑了数字j ,则继续
        • vis[j]更新为true并将变量currNum初始化为j计数0。
        • currNum % i等于0时,将currNum除以i ,增加div[j]计数1。
        • 如果currNum等于1,count等于3divs[j]等于3,cnt1。
        • 否则,如果currNum不等于1,count等于1,divs[j]等于1,divs[currNum]等于0,cnt1。
        • 如果cnt等于N,则打印j并返回

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the nth number which
// has exactly 4 divisors
int nthNumber(int n)
{
 
    // The divs[] array to store number of
    // divisors of every element
    int divs[1000000];
 
    // The vis[] array to check if given number
    // is considered or not
    bool vis[1000000];
 
    // The cnt stores number of elements having
    // exactly 4 divisors
    int cnt = 0;
 
    // Iterate while cnt less than n
    for (int i = 2; cnt < n; i++) {
 
        // If i is a prime
        if (divs[i] == 0) {
 
            // Iterate in the range [2*i, 1000000] with
            // increment of i
            for (int j = 2 * i; j < 1000000; j += i) {
 
                // If the number j is already considered
                if (vis[j]) {
                    continue;
                }
 
                vis[j] = 1;
 
                int currNum = j;
                int count = 0;
 
                // Dividing currNum by i until currNum % i is
                // equal to 0
                while (currNum % i == 0) {
                    divs[j]++;
                    currNum = currNum / i;
                    count++;
                }
 
                // Case a single prime in its factorization
                if (currNum == 1 && count == 3 && divs[j] == 3) {
                    cnt++;
                }
 
                else if (currNum != 1
                         && divs[currNum] == 0
                         && count == 1
                         && divs[j] == 1) {
                    // Case of two distinct primes which
                    // divides j exactly once each
                    cnt++;
                }
 
                if (cnt == n) {
                    return j;
                }
            }
        }
    }
 
    return -1;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 24;
 
    // Function Call
    cout << nthNumber(N) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to find the nth number which
    // has exactly 4 divisors
    static int nthNumber(int n)
    {
 
        // The divs[] array to store number of
        // divisors of every element
        int divs[] = new int[1000000];
 
        // The vis[] array to check if given number
        // is considered or not
        boolean vis[] = new boolean[1000000];
 
        // The cnt stores number of elements having
        // exactly 4 divisors
        int cnt = 0;
 
        // Iterate while cnt less than n
        for (int i = 2; cnt < n; i++) {
 
            // If i is a prime
            if (divs[i] == 0) {
 
                // Iterate in the range [2*i, 1000000] with
                // increment of i
                for (int j = 2 * i; j < 1000000; j += i) {
 
                    // If the number j is already considered
                    if (vis[j]) {
                        continue;
                    }
 
                    vis[j] = true;
 
                    int currNum = j;
                    int count = 0;
 
                    // Dividing currNum by i until currNum %
                    // i is equal to 0
                    while (currNum % i == 0) {
                        divs[j]++;
                        currNum = currNum / i;
                        count++;
                    }
 
                    // Case a single prime in its
                    // factorization
                    if (currNum == 1 && count == 3
                        && divs[j] == 3) {
                        cnt++;
                    }
 
                    else if (currNum != 1
                             && divs[currNum] == 0
                             && count == 1
                             && divs[j] == 1) {
                        // Case of two distinct primes which
                        // divides j exactly once each
                        cnt++;
                    }
 
                    if (cnt == n) {
                        return j;
                    }
                }
            }
        }
 
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int N = 24;
 
        // Function Call
        System.out.println(nthNumber(N));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to find the nth number which
# has exactly 4 divisors
def nthNumber(n):
 
    # The divs[] array to store number of
    # divisors of every element
    divs = [0 for i in range(1000000)];
 
    # The vis[] array to check if given number
    # is considered or not
    vis = [0 for i in range(1000000)];
 
    # The cnt stores number of elements having
    # exactly 4 divisors
    cnt = 0;
 
    # Iterate while cnt less than n
    for i in range(2, n):
 
        # If i is a prime
        if (divs[i] == 0):
 
            # Iterate in the range [2*i, 1000000] with
            # increment of i
            for j in range(2 * i, 1000000):
 
                # If the number j is already considered
                if (vis[j]):
                    continue;
 
                vis[j] = 1;
 
                currNum = j;
                count = 0;
 
                # Dividing currNum by i until currNum % i is
                # equal to 0
                while (currNum % i == 0):
                    divs[j] += 1;
                    currNum = currNum // i;
                    count += 1;
     
                # Case a single prime in its factorization
                if (currNum == 1 and count == 3 and divs[j] == 3):
                    cnt += 1
                elif (currNum != 1
                    and divs[currNum] == 0
                    and count == 1
                    and divs[j] == 1):
                 
                    # Case of two distinct primes which
                    # divides j exactly once each
                    cnt += 1
 
                if (cnt == n):
                    return j;
 
    return -1;
 
# Driver Code
 
# Given Input
N = 24;
 
# Function Call
print(nthNumber(N));
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
 
// Function to find minimum number of
// elements required to obtain sum K
class GFG{
 
// Function to find the nth number which
// has exactly 4 divisors
static int nthNumber(int n)
{
     
    // The divs[] array to store number of
    // divisors of every element
    int[] divs = new int[1000000];
 
    // The vis[] array to check if given number
    // is considered or not
    int[] vis  = new int[1000000];
 
    // The cnt stores number of elements having
    // exactly 4 divisors
    int cnt = 0;
 
    // Iterate while cnt less than n
    for(int i = 2; cnt < n; i++)
    {
         
        // If i is a prime
        if (divs[i] == 0)
        {
             
            // Iterate in the range [2*i, 1000000] with
            // increment of i
            for(int j = 2 * i; j < 1000000; j += i)
            {
                 
                // If the number j is already considered
                if (vis[j] != 0)
                {
                    continue;
                }
 
                vis[j] = 1;
 
                int currNum = j;
                int count = 0;
 
                // Dividing currNum by i until currNum % i is
                // equal to 0
                while (currNum % i == 0)
                {
                    divs[j]++;
                    currNum = currNum / i;
                    count++;
                }
 
                // Case a single prime in its factorization
                if (currNum == 1 && count == 3 && divs[j] == 3)
                {
                    cnt++;
                }
 
                else if (currNum != 1 && divs[currNum] == 0 &&
                           count == 1 && divs[j] == 1)
                {
                     
                    // Case of two distinct primes which
                    // divides j exactly once each
                    cnt++;
                }
 
                if (cnt == n)
                {
                    return j;
                }
            }
        }
    }
    return -1;
}
 
// Driver Code
static public void Main ()
{
     
    // Given Input
    int N = 24;
 
    // Function Call
    Console.Write(nthNumber(N));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
94

时间复杂度: O(Nlog(log(N))),其中N1000000
辅助空间: O(N)