📜  备用素数直到N

📅  最后修改于: 2021-04-29 04:01:42             🧑  作者: Mango

我们必须打印备用质数,直到N。
例子:

Input : N = 10
Output : 2 5 

Input : N = 15
Output : 2 5 11 

天真的方法:我们可以简单地对N进行迭代,并检查数字是否为质数,然后仅通过保留一个简单的alter flag变量就可以打印备用数字。

C++
/* C++ program to print all
primes smaller than or
equal to n using Naive approach.*/
#include
using namespace std;
 
/* Function for checking
number is prime or not */
int prime(int num)
{
    int i, flag = 0;
    for(i = 2; i<= num / 2; i++)
    {
        if(num % i == 0)
        {
            flag = 1;
            break;
        }
    }
     
    // if flag = 0 then number
    // is prime and return 1
    // otherwise return 0
    if(flag == 0)
        return 1;
    else
        return 0;
}
 
// Function for printing
// alternate prime number
void print_alternate_prime(int n)
{
    // counter is initialize with 0
    int counter = 0;
 
    // looping through 2 to n-1
    for(int num = 2; num < n; num++)
    {
        // function calling along
        // with if condition
        if (prime(num) == 1)
        {
            // if counter is multiple of 2
            // then only print prime number
            if (counter % 2 == 0)
                cout << num << " ";
                 
            counter ++;
        }
    }
}
 
// Driver code
int main()
{
    int n = 15;
    cout << "Following are the alternate prime"
         << " number smaller than or equal to "
         << n << endl;
          
    // Function calling
    print_alternate_prime(n);
}
         
// This code is contributed
// by ChitraNayal


Java
// Java program to print all
// primes smaller than or
// equal to n using Naive approach.
class GFG
{
     
/* Function for checking
number is prime or not */
static int prime(int num)
{
int i, flag = 0;
for(i = 2; i<= num / 2; i++)
{
    if(num % i == 0)
    {
        flag = 1;
        break;
    }
}
 
// if flag = 0 then number is prime
// and return 1 otherwise return 0
if(flag == 0)
    return 1;
else
    return 0;
}
 
// Function for printing
// alternate prime number
static void print_alternate_prime(int n)
{
// counter is initialize with 0
int counter = 0;
 
// looping through 2 to n-1
for(int num = 2; num < n; num++)
{
    // function calling along
    // with if condition
    if (prime(num) == 1)
    {
        // if counter is multiple of 2
        // then only print prime number
        if (counter % 2 == 0)
            System.out.print(num + " ");
                 
        counter ++;
    }
}
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
    System.out.println("Following are the alternate " +
                         "prime number smaller than " +
                                   "or equal to " + n);
 
    // Function calling
    print_alternate_prime(n);
}
}
 
// This code is contributed
// by ChitraNayal


Python3
# Python3 program to print all
# primes smaller than or
# equal to n using Naive approach.
 
# Function for checking number
# is prime or not
def prime(num) :
    flag = 0
    for i in range(2,num // 2 + 1) :
        if num % i == 0 :
            flag = 1
            break
    # if flag = 0 then number is prime
    # and return 1 otherwise return 0
    if flag == 0 :
        return 1
    else :
        return 0
 
# Function for printing alternate prime number
def print_alternate_prime(n):
     
    # counter is initialize with 0
    counter = 0
 
    # looping through 2 to n-1
    for num in range(2,n) :
         
        # function calling along with if condition
        if prime(num) == 1 :
             
            # if counter is multiple of 2 then
            # only print prime number
            if counter % 2 == 0 :
                print(num,end =" ")
                 
            counter += 1
 
# Driver code
if __name__ == "__main__":
    n = 15
    print("Following are the alternate prime"
          +"number smaller than or equal to",n)
 
          
 
    # Function calling
    print_alternate_prime(n)


C#
// C# program to print all
// primes smaller than or
// equal to n using Naive approach.
using System;
class GFG
{
/* Function for checking
number is prime or not */
static int prime(int num)
{
int i, flag = 0;
for(i = 2; i <= num / 2; i++)
{
    if(num % i == 0)
    {
        flag = 1;
        break;
    }
}
 
// if flag = 0 then number is prime
// and return 1 otherwise return 0
if(flag == 0)
    return 1;
else
    return 0;
}
 
// Function for printing
// alternate prime number
static void print_alternate_prime(int n)
{
// counter is initialize with 0
int counter = 0;
 
// looping through 2 to n-1
for(int num = 2; num < n; num++)
{
    // function calling along
    // with if condition
    if (prime(num) == 1)
    {
        // if counter is multiple of 2
        // then only print prime number
        if (counter % 2 == 0)
            Console.Write(num + " ");
                 
        counter ++;
    }
}
}
 
// Driver code
public static void Main()
{
    int n = 15;
    Console.Write("Following are the alternate " +
                    "prime number smaller than " +
                       "or equal to " + n + "\n");
 
    // Function calling
    print_alternate_prime(n);
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


C++
// C++ program to print all primes smaller than or
// equal to n using Sieve of Eratosthenes
#include 
using namespace std;
 
void SieveOfEratosthenes(int n)
{
    // 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 + 1];
    memset(prime, true, sizeof(prime));
 
    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;
        }
    }
 
    // Print all prime numbers
    bool flag = true;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            if (flag) {
                cout << p << " ";
                flag = false;
            }
            else {
 
                // for next prime to get printed
                flag = true;
            }
        }
    }
}
 
// Driver Program to test above function
int main()
{
    int n = 15;
    cout << "Following are the alternate"
         << " prime numbers smaller "
         << " than or equal to " << n << endl;
    SieveOfEratosthenes(n);
    return 0;
}


Java
// Java program to print all primes
// smaller than or equal to n using
// Sieve of Eratosthenes
class GFG
{
static void SieveOfEratosthenes(int n)
{
// 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.
boolean []prime = new boolean[n + 1];
for(int i = 0; i < prime.length; i++)
    prime[i] = true;
 
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;
    }
}
 
// Print all prime numbers
boolean flag = true;
for (int p = 2; p <= n; p++)
{
    if (prime[p])
    {
        if (flag)
        {
            System.out.print(p + " ");
            flag = false;
        }
        else
        {
 
            // for next prime
            // to get printed
            flag = true;
        }
    }
}
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 15;
    System.out.println("Following are the alternate" +
                           " prime numbers smaller " +
                            "than or equal to " + n );
    SieveOfEratosthenes(n);
}
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to print all
# equal to n using Sieve of Eratosthenes
 
def SieveOfEratosthenes(n):
 
    # 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 = [None] * (n + 1)
    for i in range(len(prime)):
        prime[i] = True
 
    p = 2
    while p * p <= 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(p * 2, n + 1, p):
                prime[i] = False
                 
        p += 1
 
    # Print all prime numbers
    flag = True
    for p in range(2, n + 1):
        if (prime[p]):
            if (flag):
                print(str(p), end = " ")
                flag = False
             
            else:
 
                # for next prime to get printed
                flag = True
 
# Driver Code
if __name__ == "__main__":
    n = 15
    print("Following are the alternate" +
              " prime numbers smaller " +
            "than or equal to " + str(n))
    SieveOfEratosthenes(n)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to print all primes
// smaller than or equal to n using
// Sieve of Eratosthenes
using System;
 
class GFG
{
static void SieveOfEratosthenes(int n)
{
     
// Create a bool 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 = new bool[n + 1];
for(int i = 0; i < prime.Length; i++)
    prime[i] = true;
 
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;
    }
}
 
// Print all prime numbers
bool flag = true;
for (int p = 2; p <= n; p++)
{
    if (prime[p])
    {
        if (flag)
        {
            Console.Write(p + " ");
            flag = false;
        }
        else
        {
 
            // for next prime to
            // get printed
            flag = true;
        }
    }
}
}
 
// Driver Code
public static void Main()
{
    int n = 15;
    Console.Write("Following are the alternate" +
                      " prime numbers smaller " +
                 "than or equal to " + n + "\n");
    SieveOfEratosthenes(n);
}
}
 
// This code is contributed
// by ChitraNayal


PHP


输出:
Following are the alternate prime numbers smaller  than or equal to 15
2 5 11

时间复杂度: O(N *    )
高效的方法:使用Eratosthenes筛子,我们可以在筛子中打印所有替代的真实值

C++

// C++ program to print all primes smaller than or
// equal to n using Sieve of Eratosthenes
#include 
using namespace std;
 
void SieveOfEratosthenes(int n)
{
    // 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 + 1];
    memset(prime, true, sizeof(prime));
 
    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;
        }
    }
 
    // Print all prime numbers
    bool flag = true;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            if (flag) {
                cout << p << " ";
                flag = false;
            }
            else {
 
                // for next prime to get printed
                flag = true;
            }
        }
    }
}
 
// Driver Program to test above function
int main()
{
    int n = 15;
    cout << "Following are the alternate"
         << " prime numbers smaller "
         << " than or equal to " << n << endl;
    SieveOfEratosthenes(n);
    return 0;
}

Java

// Java program to print all primes
// smaller than or equal to n using
// Sieve of Eratosthenes
class GFG
{
static void SieveOfEratosthenes(int n)
{
// 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.
boolean []prime = new boolean[n + 1];
for(int i = 0; i < prime.length; i++)
    prime[i] = true;
 
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;
    }
}
 
// Print all prime numbers
boolean flag = true;
for (int p = 2; p <= n; p++)
{
    if (prime[p])
    {
        if (flag)
        {
            System.out.print(p + " ");
            flag = false;
        }
        else
        {
 
            // for next prime
            // to get printed
            flag = true;
        }
    }
}
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 15;
    System.out.println("Following are the alternate" +
                           " prime numbers smaller " +
                            "than or equal to " + n );
    SieveOfEratosthenes(n);
}
}
 
// This code is contributed
// by ChitraNayal

的Python 3

# Python 3 program to print all
# equal to n using Sieve of Eratosthenes
 
def SieveOfEratosthenes(n):
 
    # 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 = [None] * (n + 1)
    for i in range(len(prime)):
        prime[i] = True
 
    p = 2
    while p * p <= 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(p * 2, n + 1, p):
                prime[i] = False
                 
        p += 1
 
    # Print all prime numbers
    flag = True
    for p in range(2, n + 1):
        if (prime[p]):
            if (flag):
                print(str(p), end = " ")
                flag = False
             
            else:
 
                # for next prime to get printed
                flag = True
 
# Driver Code
if __name__ == "__main__":
    n = 15
    print("Following are the alternate" +
              " prime numbers smaller " +
            "than or equal to " + str(n))
    SieveOfEratosthenes(n)
 
# This code is contributed
# by ChitraNayal

C#

// C# program to print all primes
// smaller than or equal to n using
// Sieve of Eratosthenes
using System;
 
class GFG
{
static void SieveOfEratosthenes(int n)
{
     
// Create a bool 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 = new bool[n + 1];
for(int i = 0; i < prime.Length; i++)
    prime[i] = true;
 
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;
    }
}
 
// Print all prime numbers
bool flag = true;
for (int p = 2; p <= n; p++)
{
    if (prime[p])
    {
        if (flag)
        {
            Console.Write(p + " ");
            flag = false;
        }
        else
        {
 
            // for next prime to
            // get printed
            flag = true;
        }
    }
}
}
 
// Driver Code
public static void Main()
{
    int n = 15;
    Console.Write("Following are the alternate" +
                      " prime numbers smaller " +
                 "than or equal to " + n + "\n");
    SieveOfEratosthenes(n);
}
}
 
// This code is contributed
// by ChitraNayal

的PHP


输出:
Following are the alternate prime numbers smaller  than or equal to 15
2 5 11

时间复杂度: O(    * log(log(N)))用于进行筛分O(N)用于进行筛分。