📜  第N个智能号码

📅  最后修改于: 2021-05-04 13:41:03             🧑  作者: Mango

给定数字n,找到第n个智能数(1 <= n <= 1000)。智能数是具有至少三个不同素数的数字。我们将结果值的上限设为MAX

例如30是第一个智能数,因为它有2、3、5作为独特的素数。 42是第二个智能数,因为它有2、3、7作为独特的素数。
例子:

Input : n = 1
Output: 30
// three distinct prime factors 2, 3, 5

Input : n = 50
Output: 273
// three distinct prime factors 3, 7, 13

Input : n = 1000
Output: 2664
// three distinct prime factors 2, 3, 37

这个想法是基于Eratosthenes筛。我们使用数组来使用数组prime []来跟踪素数。我们还使用相同的数组来跟踪到目前为止看到的主要因素的数量。每当计数达到3时,我们都会将结果相加。

  • 取一个数组primes []并将其初始化为0。
  • 现在我们知道第一个素数是i = 2,因此标记primes [2] = 1即; primes [i] = 1表示’i’是素数。
  • 现在遍历primes []数组,并用条件primes [j]-= 1标记’i’的所有倍数,其中’j’是’i’的倍数,并检查条件primes [j] +3 = 0,因为无论何时素数[j]变为-3表示以前它是三个不同素数的倍数。如果条件primes [j] + 3 = 0成立,则意味着’j’是一个智能数,因此将其存储在数组result []中。
  • 现在对数组result []排序并返回result [n-1]。

以下是上述想法的实现。

C++
// C++ implementation to find n'th smart number
#include
using namespace std;
  
// Limit on result
const int MAX = 3000;
  
// Function to calculate n'th smart number
int smartNumber(int n)
{
    // Initialize all numbers as not prime
    int primes[MAX] = {0};
  
    // iterate to mark all primes and smart number
    vector result;
  
    // Traverse all numbers till maximum limit
    for (int i=2; i


Java
// Java implementation to find n'th smart number
import java.util.*;
import java.lang.*;
  
class GFG {
  
    // Limit on result
    static int MAX = 3000;
  
    // Function to calculate n'th smart number
    public static int smartNumber(int n)
    {
          
        // Initialize all numbers as not prime
        Integer[] primes = new Integer[MAX];
        Arrays.fill(primes, new Integer(0));
  
        // iterate to mark all primes and smart
        // number
        Vector result = new Vector<>();
  
        // Traverse all numbers till maximum
        // limit
        for (int i = 2; i < MAX; i++)
        {
              
            // 'i' is maked as prime number
            // because it is not multiple of
            // any other prime
            if (primes[i] == 0)
            {
                primes[i] = 1;
  
                // mark all multiples of 'i' 
                // as non prime
                for (int j = i*2; j < MAX; j = j+i)
                {
                    primes[j] -= 1;
      
                    // If i is the third prime
                    // factor of j then add it
                    // to result as it has at
                    // least three prime factors.
                    if ( (primes[j] + 3) == 0)
                        result.add(j);
                }
            }
        }
  
        // Sort all smart numbers
        Collections.sort(result);
  
        // return n'th smart number
        return result.get(n-1);
  
    }
  
    // Driver program to run the case
    public static void main(String[] args)
    {
        int n = 50;
        System.out.println(smartNumber(n));
  
    }
}
  
// This code is contributed by Prasad Kshirsagar


Python3
# Python3 implementation to find
# n'th smart number 
  
# Limit on result 
MAX = 3000; 
  
# Function to calculate n'th
# smart number 
def smartNumber(n): 
  
    # Initialize all numbers as not prime 
    primes = [0] * MAX; 
  
    # iterate to mark all primes 
    # and smart number 
    result = []; 
  
    # Traverse all numbers till maximum limit 
    for i in range(2, MAX): 
          
        # 'i' is maked as prime number because 
        # it is not multiple of any other prime 
        if (primes[i] == 0): 
            primes[i] = 1; 
  
            # mark all multiples of 'i' as non prime
            j = i * 2;
            while (j < MAX): 
                primes[j] -= 1; 
  
                # If i is the third prime factor of j 
                # then add it to result as it has at 
                # least three prime factors. 
                if ( (primes[j] + 3) == 0): 
                    result.append(j);
                j = j + i;
  
    # Sort all smart numbers 
    result.sort(); 
  
    # return n'th smart number 
    return result[n - 1]; 
  
# Driver Code
n = 50; 
print(smartNumber(n)); 
  
# This code is contributed by mits


C#
// C# implementation to find n'th smart number
using System.Collections.Generic;
  
class GFG {
  
    // Limit on result
    static int MAX = 3000;
  
    // Function to calculate n'th smart number
    public static int smartNumber(int n)
    {
          
        // Initialize all numbers as not prime
        int[] primes = new int[MAX];
  
        // iterate to mark all primes and smart
        // number
        List result = new List();
  
        // Traverse all numbers till maximum
        // limit
        for (int i = 2; i < MAX; i++)
        {
              
            // 'i' is maked as prime number
            // because it is not multiple of
            // any other prime
            if (primes[i] == 0)
            {
                primes[i] = 1;
  
                // mark all multiples of 'i' 
                // as non prime
                for (int j = i*2; j < MAX; j = j+i)
                {
                    primes[j] -= 1;
      
                    // If i is the third prime
                    // factor of j then add it
                    // to result as it has at
                    // least three prime factors.
                    if ( (primes[j] + 3) == 0)
                        result.Add(j);
                }
            }
        }
  
        // Sort all smart numbers
        result.Sort();
  
        // return n'th smart number
        return result[n-1];
  
    }
  
    // Driver program to run the case
    public static void Main()
    {
        int n = 50;
        System.Console.WriteLine(smartNumber(n));
  
    }
}
  
// This code is contributed by mits


PHP


输出:

273