📜  查找小于或等于给定数字的最大特殊素数

📅  最后修改于: 2021-04-30 02:35:37             🧑  作者: Mango

给定数字N。任务是找到小于或等于N的最大特殊质数。

特殊质数是一个数字,可以通过将数字一个接一个地放置来创建,这样所有产生的数字都是质数。

例子

Input : N = 379
Output : 379
Explanation: 379 can be created as => 3 => 37 => 379
Here, all the numbers ie. 3, 37, 379 are prime.

Input : N = 100
Output : 79
Explanation: 79 can be created as => 7 => 79, 
where both 7, 79 are prime numbers.

方法:这个想法是使用Eratosthenes筛。构造筛网阵列,直到数字N。然后从数字N开始迭代返回,以检查数字是否为质数。如果是素数,请检查它是否是特殊素数。

现在,检查数字是否为特殊质数。继续将数字除以10,然后在每个点检查剩余的数字是否为质数,这可以通过引用已构建的Sieve数组来完成。

下面是上述方法的实现:

C++
// CPP program to find the Largest Special Prime
// which is less than or equal to a given number
  
#include 
using namespace std;
  
// Function to check whether the number
// is a special prime or not
bool checkSpecialPrime(bool* sieve, int num)
{
    // While number is not equal to zero
    while (num) {
        // If the number is not prime
        // return false.
        if (!sieve[num]) {
            return false;
        }
  
        // Else remove the last digit
        // by dividing the number by 10.
        num /= 10;
    }
  
    // If the number has become zero
    // then the number is special prime,
    // hence return true
    return true;
}
  
// Function to find the Largest Special Prime
// which is less than or equal to a given number
void findSpecialPrime(int N)
{
    bool sieve[N + 10];
  
    // Initially all numbers are considered Primes.
    memset(sieve, true, sizeof(sieve));
    sieve[0] = sieve[1] = false; 
    for (long long i = 2; i <= N; i++) {
        if (sieve[i]) {
  
            for (long long j = i * i; j <= N; j += i) {
                sieve[j] = false;
            }
        }
    }
  
    // There is always an answer possible
    while (true) {
        // Checking if the number is a
        // special prime or not
        if (checkSpecialPrime(sieve, N)) {
            // If yes print the number
            // and break the loop.
            cout << N << '\n';
            break;
        }
        // Else decrement the number.
        else
            N--;
    }
}
  
// Driver code
int main()
{
    findSpecialPrime(379);
    findSpecialPrime(100);
  
    return 0;
}


Java
// Java program to find the Largest Special Prime
// which is less than or equal to a given number
  
class GFG
{
  
        // Function to check whether the number
        // is a special prime or not
    static boolean checkSpecialPrime(boolean [] sieve, int num)
        {
            // While number is not equal to zero
            while (num!=0) {
                // If the number is not prime
                // return false.
                if (!sieve[num]) {
                    return false;
                }
          
                // Else remove the last digit
                // by dividing the number by 10.
                num /= 10;
            }
          
            // If the number has become zero
            // then the number is special prime,
            // hence return true
            return true;
        }
          
        // Function to find the Largest Special Prime
        // which is less than or equal to a given number
        static void findSpecialPrime(int N)
        {
            boolean []sieve=new boolean[N+10];
            sieve[0] = sieve[1] = false;
  
            // Initially all numbers are considered Primes.
            for(int i=0;i


Python 3
# Python 3 program to find the Largest 
# Special Prime which is less than or
# equal to a given number
  
# Function to check whether the number
# is a special prime or not
def checkSpecialPrime(sieve, num):
  
    # While number is not equal to zero
    while (num) :
          
        # If the number is not prime
        # return false.
        if (not sieve[num]) :
            return False
  
        # Else remove the last digit
        # by dividing the number by 10.
        num //= 10
  
    # If the number has become zero
    # then the number is special prime,
    # hence return true
    return True
  
# Function to find the Largest Special 
# Prime which is less than or equal to 
# a given number
def findSpecialPrime(N):
  
    # Initially all numbers are
    # considered Primes.
    sieve = [True] * (N + 10)
    sieve[0] = sieve[1] = False;
    for i in range(2, N + 1) :
        if (sieve[i]) :
  
            for j in range(i * i, N + 1, i) :
                sieve[j] = False
  
    # There is always an answer possible
    while (True) :
          
        # Checking if the number is 
        # a special prime or not
        if (checkSpecialPrime(sieve, N)):
              
            # If yes print the number
            # and break the loop.
            print( N) 
            break
              
        # Else decrement the number.
        else:
            N -= 1
  
# Driver code
if __name__ == "__main__":
    findSpecialPrime(379)
    findSpecialPrime(100)
  
# This code is contributed 
# by ChitraNayal


C#
// C# program to find the Largest Special Prime
// which is less than or equal to a given number
  
using System;
class GFG
{
  
        // Function to check whether the number
        // is a special prime or not
    static bool checkSpecialPrime(bool [] sieve, int num)
        {
            // While number is not equal to zero
            while (num!=0) {
                // If the number is not prime
                // return false.
                if (!sieve[num]) {
                    return false;
                }
          
                // Else remove the last digit
                // by dividing the number by 10.
                num /= 10;
            }
          
            // If the number has become zero
            // then the number is special prime,
            // hence return true
            return true;
        }
          
        // Function to find the Largest Special Prime
        // which is less than or equal to a given number
        static void findSpecialPrime(int N)
        {
            bool []sieve=new bool[N+10];
              
              
            // Initially all numbers are considered Primes.
            for(int i = 0; i < N + 10; i++)
                sieve[i] = true;
                  
            sieve[0] = sieve[1] = false;
            for (int i = 2; i <= N; i++) {
                if (sieve[i]) {
          
                    for ( int j = i * i; j <= N; j += i) {
                        sieve[j] = false;
                    }
                }
            }
          
            // There is always an answer possible
            while (true) {
                // Checking if the number is a
                // special prime or not
                if (checkSpecialPrime(sieve, N)) {
                    // If yes print the number
                    // and break the loop.
                    Console.WriteLine(N);
                    break;
                }
                // Else decrement the number.
                else
                    N--;
            }
        }
          
        // Driver code
        public static void Main()
        {
            findSpecialPrime(379);
            findSpecialPrime(100);
          
              
        }
  
// This code is contributed by ihritik
  
}


PHP


输出:
379
79

时间复杂度: O(N * log(log N))