📜  查找从1到N的几乎素数的数量

📅  最后修改于: 2021-04-29 04:34:16             🧑  作者: Mango

给定数字N。找到从1到的几乎素数的数量n 。如果一个数字恰好具有两个截然不同的素数,则称其为“几乎”。

注意:数字可以具有任意数量的非质数,但应恰好具有两个质数。

例子

Input : N = 10
Output : 2
Explanation : 6, 10 are such numbers.

Input : N = 21
Output : 8

一种有效的解决方案是使用Eratosthenes筛子查找素数。并找到不同的素数来计算小于N的数。

请参考:几乎素数

下面是上述方法的实现:

C++
// CPP program to count almost prime numbers
// from 1 to n
#include 
using namespace std;
#define N 100005
  
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[N];
  
void SieveOfEratosthenes()
{
    memset(prime, true, sizeof(prime));
    prime[1] = false;
  
    for (int p = 2; p * p < N; p++) {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) {
            // Update all multiples of p
            for (int i = p * 2; i < N; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to count almost prime numbers
// from 1 to n
int almostPrimes(int n)
{
    // to store required answer
    int ans = 0;
  
    // 6 is first almost prime number
    for (int i = 6; i <= n; i++) {
        // to count prime factors
        int c = 0;
        for (int j = 2; j * j <= i; j++) {
            if (i % j == 0) {
                // if it is perfect square
                if (j * j == i) {
                    if (prime[j])
                        c++;
                }
                else {
                    if (prime[j])
                        c++;
                    if (prime[i / j])
                        c++;
                }
            }
        }
  
        // if I is almost prime number
        if (c == 2)
            ans++;
    }
    return ans;
}
  
// Driver code
int main()
{
    SieveOfEratosthenes();
    int n = 21;
  
    cout << almostPrimes(n);
  
    return 0;
}


Java
// Java program to count almost prime numbers
// from 1 to n
  
import java.io.*;
  
class GFG {
  
static int N = 100005;
  
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
static boolean prime[] = new boolean[N];
static void SieveOfEratosthenes()
{
    for(int i=0;i


Python 3
# Python 3 program to count almost 
# prime numbers 
# from 1 to n 
  
# from math import everything
from math import *
  
N = 100005
  
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true. 
# A value in prime[i] will 
# finally be false if i is Not a prime, else true. 
prime = [True] * N
  
def SieveOfEratosthenes() :
  
    prime[1] = False
  
    for p in range(2, int(sqrt(N))) :
  
        # If prime[p] is not changed, then 
        # it is a prime 
        if prime[p] == True :
  
            # Update all multiples of p 
            for i in range(2*p, N, p) :
                prime[i] = False
  
  
# Function to count almost prime numbers 
# from 1 to n 
def almostPrimes(n) :
  
    # to store required answer
    ans = 0
  
    # 6 is first almost prime number 
    for i in range(6, n + 1) :
  
        # to count prime factors 
        c = 0
        for j in range(2, int(sqrt(i)) + 1) :
  
            # if it is perfect square
            if i % j == 0 :
  
                if j * j == i :
                    if prime[j] :
                        c += 1
                else :
                    if prime[j] :
                        c += 1
                    if prime[i // j] :
                        c += 1
  
        # if I is almost prime number 
        if c == 2 :
            ans += 1
  
    return ans
      
      
# Driver Code
if __name__ == "__main__" :
  
    SieveOfEratosthenes()
    n = 21
  
    print(almostPrimes(n))
      
# This code is contributed by ANKITRAI1


C#
// C# program to count almost 
// prime numbers from 1 to n
using System;
  
class GFG
{
  
static int N = 100005;
  
// Create a boolean array "prime[0..n]" 
// and initialize all entries it as 
// true. A value in prime[i] will finally
// be false if i is Not a prime, else true.
static bool []prime = new bool[N];
static void SieveOfEratosthenes()
{
    for(int i = 0; i < N; i++)
    prime[i] = true;
    prime[1] = false;
  
    for (int p = 2; p * p < N; p++)
    {
        // If prime[p] is not changed, 
        // then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p
            for (int i = p * 2; i < N; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to count almost
// prime numbers from 1 to n
static int almostPrimes(int n)
{
    // to store required answer
    int ans = 0;
  
    // 6 is first almost prime number
    for (int i = 6; i <= n; i++)
    {
        // to count prime factors
        int c = 0;
        for (int j = 2; j * j <= i; j++) 
        {
            if (i % j == 0)
            {
                // if it is perfect square
                if (j * j == i)
                {
                    if (prime[j])
                        c++;
                }
                else 
                {
                    if (prime[j])
                        c++;
                    if (prime[i / j])
                        c++;
                }
            }
        }
  
        // if I is almost prime number
        if (c == 2)
            ans++;
    }
    return ans;
}
  
// Driver code
public static void Main () 
{
    SieveOfEratosthenes();
    int n = 21;
      
    Console.WriteLine( almostPrimes(n));
}
}
  
// This code is contributed 
// by inder_verma


PHP
= $j * $j; $j++)
        { 
  
            // if it is perfect square
            if ($i % $j == 0)
            {
                if ($j * $j == $i)
                {
                    if ($prime[$j])
                        $c += 1;
                }
                else
                {
                    if ($prime[$j])
                        $c += 1;
                    if ($prime[($i / $j)]) 
                        $c += 1;
                }
            } 
              
        }
          
    // if I is almost prime number 
    if ($c == 2)
        $ans += 1;
    }
    return $ans;
} 
      
// Driver Code
SieveOfEratosthenes();
$n = 21;
  
print(almostPrimes($n));
  
// This code is contributed by mits
?>


输出:
8