📌  相关文章
📜  计算给定范围内的全素数

📅  最后修改于: 2021-04-29 07:13:46             🧑  作者: Mango

给定两个整数LR ,任务是计算给定范围内存在的全质数的数量。

例子:

方法:请按照以下步骤解决问题:

  • 只需遍历从LR的范围。
  • 对于范围内的每个数字i ,请检查是否可以将其除以[2,sqrt(i)]范围内的任何数字。如果发现是真的,那么它不是素数。继续下一个数字。
  • 否则,请检查其所有数字是否都是质数。如果发现是真的,请增加count
  • 最后,在遍历范围之后,打印count的值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if a
// number is prime or not
bool isPrime(int num)
{
    if (num <= 1)
        return false;
 
    for (int i = 2; i * i <= num; i++)
 
        // If a divisor of n exists
        if (num % i == 0)
            return false;
    return true;
}
 
// Function to check if a
// number is Full Prime or not
bool isFulPrime(int n)
{
    // If n is not a prime
    if (!isPrime(n))
        return false;
 
    // Otherwise
    else {
        while (n > 0) {
            // Extract digit
            int rem = n % 10;
            // If any digit of n
            // is non-prime
            if (!(rem == 2 || rem == 3
                  || rem == 5 || rem == 7))
                return false;
 
            n = n / 10;
        }
    }
    return true;
}
 
// Function to print count of
// Full Primes in a range [L, R]
int countFulPrime(int L, int R)
{
    // Stores count of full primes
    int cnt = 0;
 
    for (int i = L; i <= R; i++) {
 
        // Check if i is full prime
        if ((i % 2) != 0 && isFulPrime(i)) {
            cnt++;
        }
    }
    return cnt;
}
 
// Driver Code
int main()
{
    int L = 1, R = 100;
 
    // Stores count of full primes
    int ans = 0;
 
    if (L < 3)
        ans++;
    cout << ans + countFulPrime(L, R);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
  
// Function to check if a
// number is prime or not
static boolean isPrime(int num)
{
    if (num <= 1)
        return false;
  
    for(int i = 2; i * i <= num; i++)
     
        // If a divisor of n exists
        if (num % i == 0)
            return false;
             
    return true;
}
  
// Function to check if a
// number is Full Prime or not
static boolean isFulPrime(int n)
{
     
    // If n is not a prime
    if (!isPrime(n))
        return false;
  
    // Otherwise
    else
    {
        while (n > 0)
        {
             
            // Extract digit
            int rem = n % 10;
             
            // If any digit of n
            // is non-prime
            if (!(rem == 2 || rem == 3 ||
                  rem == 5 || rem == 7))
                return false;
  
            n = n / 10;
        }
    }
    return true;
}
  
// Function to print count of
// Full Primes in a range [L, R]
static int countFulPrime(int L, int R)
{
     
    // Stores count of full primes
    int cnt = 0;
  
    for(int i = L; i <= R; i++)
    {
         
        // Check if i is full prime
        if ((i % 2) != 0 && isFulPrime(i))
        {
            cnt++;
        }
    }
    return cnt;
}
  
// Driver Code
public static void main (String[] args)
{
    int L = 1, R = 100;
  
    // Stores count of full primes
    int ans = 0;
  
    if (L < 3)
        ans++;
         
    System.out.println(ans + countFulPrime(L, R));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to check if a
# number is prime or not
def isPrime(num):
     
    if (num <= 1):
        return False
 
    for i in range(2, num + 1):
        if i * i > num:
            break
 
        # If a divisor of n exists
        if (num % i == 0):
            return False
             
    return True
 
# Function to check if a
# number is Full Prime or not
def isFulPrime(n):
     
    # If n is not a prime
    if (not isPrime(n)):
        return False
 
    # Otherwise
    else:
        while (n > 0):
             
            # Extract digit
            rem = n % 10
             
            # If any digit of n
            # is non-prime
            if (not (rem == 2 or rem == 3 or
                     rem == 5 or rem == 7)):
                return False
 
            n = n // 10
             
    return True
 
# Function to prcount of
# Full Primes in a range [L, R]
def countFulPrime(L, R):
     
    # Stores count of full primes
    cnt = 0
 
    for i in range(L, R + 1):
         
        # Check if i is full prime
        if ((i % 2) != 0 and isFulPrime(i)):
            cnt += 1
             
    return cnt
 
# Driver Code
if __name__ == '__main__':
     
    L = 1
    R = 100
 
    # Stores count of full primes
    ans = 0
 
    if (L < 3):
        ans += 1
         
    print(ans + countFulPrime(L, R))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
  
// Function to check if a
// number is prime or not
static bool isPrime(int num)
{
    if (num <= 1)
        return false;
  
    for(int i = 2; i * i <= num; i++)
     
        // If a divisor of n exists
        if (num % i == 0)
            return false;
             
    return true;
}
  
// Function to check if a
// number is Full Prime or not
static bool isFulPrime(int n)
{
     
    // If n is not a prime
    if (!isPrime(n))
        return false;
  
    // Otherwise
    else
    {
        while (n > 0)
        {
             
            // Extract digit
            int rem = n % 10;
             
            // If any digit of n
            // is non-prime
            if (!(rem == 2 || rem == 3 ||
                  rem == 5 || rem == 7))
                return false;
  
            n = n / 10;
        }
    }
    return true;
}
  
// Function to print count of
// Full Primes in a range [L, R]
static int countFulPrime(int L, int R)
{
     
    // Stores count of full primes
    int cnt = 0;
  
    for(int i = L; i <= R; i++)
    {
         
        // Check if i is full prime
        if ((i % 2) != 0 && isFulPrime(i))
        {
            cnt++;
        }
    }
    return cnt;
}
  
// Driver Code
public static void Main (String[] args)
{
    int L = 1, R = 100;
  
    // Stores count of full primes
    int ans = 0;
  
    if (L < 3)
        ans++;
         
    Console.WriteLine(ans + countFulPrime(L, R));
}
}
 
// This code is contributed by math_lover


输出:
8









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