原文:

给定一个数字 n,任务是检查给定的数字是否为prost prime。 一个原质数是一个是原质数。 前几个原素是–

3, 5, 13, 17, 41, 97, 113, 193, 241, 257, 353, 449, 577, 641, 673, 769, 929, 1153, 1217, … ..

例:

input: 41
output: 41 is proth prime
input: 19
output: 19 is not a proth prime

方法: 想法是使用厄拉多塞的筛子来寻找阿普顿的素数。然后检查给定的号码是否为。如果数是一个并且也是一个素数,那么给定的数就是原素数。 以下是上述算法的实现:

c

// c   implementation of the above approach
#include 
using namespace std;
int prime[1000000];
// calculate all primes upto n.
void sieveoferatosthenes(int n)
{
    // initialize all entries it as true.
    // a value in prime[i] will finally
    // false if i is not a prime, else true.
    for (int i = 1; i <= n   1; 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
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i  = p)
                prime[i] = false;
        }
    }
}
// utility function to check power of two
bool ispoweroftwo(int n)
{
    return (n && !(n & (n - 1)));
}
// function to check if the given
// number is proth number or not
bool isprothnumber(int n)
{
    int k = 1;
    while (k < (n / k)) {
        // check if k divides n or not
        if (n % k == 0) {
            // check if n/k is power of 2 or not
            if (ispoweroftwo(n / k))
                return true;
        }
        // update k to next odd number
        k = k   2;
    }
    // if we reach here means there
    // exists no value of k such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
// function to check whether the given
// number is proth prime or not.
bool isprothprime(int n)
{
    // check n for proth number
    if (isprothnumber(n - 1)) {
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
// driver code
int main()
{
    int n = 41;
    // if number is proth number,
    // calculate primes upto n
    sieveoferatosthenes(n);
    for (int i = 1; i <= n; i  )
        // check n for proth prime
        if (isprothprime(i))
            cout << i << endl;
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the above approach
import java.util.*;
class gfg
{
static boolean[] prime = new boolean[1000000];
// calculate all primes upto n.
static void sieveoferatosthenes(int n)
{
    // initialize all entries it as true.
    // a value in prime[i] will finally
    // false if i is not a prime, else true.
    for (int i = 1; i <= n   1; 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
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i  = p)
                prime[i] = false;
        }
    }
}
// utility function to check power of two
static boolean ispoweroftwo(int n)
{
    return (n > 0 && (n & (n - 1)) == 0);
}
// function to check if the given
// number is proth number or not
static boolean isprothnumber(int n)
{
    int k = 1;
    while (k < (int)(n / k))
    {
        // check if k divides n or not
        if (n % k == 0)
        {
            // check if n/k is power of 2 or not
            if (ispoweroftwo((int)(n / k)))
                return true;
        }
        // update k to next odd number
        k = k   2;
    }
    // if we reach here means there
    // exists no value of k such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
// function to check whether the given
// number is proth prime or not.
static boolean isprothprime(int n)
{
    // check n for proth number
    if (isprothnumber(n - 1))
    {
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
// driver code
public static void main(string args[])
{
    int n = 41;
    // if number is proth number,
    // calculate primes upto n
    sieveoferatosthenes(n);
    for (int i = 1; i <= n; i  )
        // check n for proth prime
        if (isprothprime(i))
            system.out.println(i);
}
}
// this code is contributed by
// surendra_gangwar

python 3

# python3 implementation of the
# above approach
import math as mt
prime = [0 for i in range(1000000)]
# calculate all primes upto n.
def sieveoferatosthenes(n):
    # initialize all entries it as true.
    # a value in prime[i] will finally
    # false if i is not a prime, else true.
    for i in range(1, n   2):
        prime[i] = true
    prime[1] = false
    for p in range(2, mt.ceil(n**(0.5))):
        # if prime[p] is not changed,
        # then it is a prime
        if (prime[p] == true):
            # update all multiples of p
            # greater than or equal to
            # the square of it numbers
            # which are multiple of p and are
            # less than p^2 are already been marked.
            for i in range(p * p, n   1, p):
                prime[i] = false
# utility function to check power of two
def ispoweroftwo(n):
    return (n and (n & (n - 1)) == false)
# function to check if the given
# number is proth number or not
def isprothnumber(n):
    k = 1
    while (k < (n // k)):
        # check if k divides n or not
        if (n % k == 0):
            # check if n/k is power of 2 or not
            if (ispoweroftwo(n // k)):
                return true
        # update k to next odd number
        k = k   2
    # if we reach here means there
    # exists no value of k such
    # that k is odd number and n/k
    # is a power of 2 greater than k
    return false
# function to check whether the given
# number is proth prime or not.
def isprothprime(n):
    # check n for proth number
    if (isprothnumber(n - 1)):
        # if number is prime, return true
        if (prime[n]):
            return true
        else:
            return false
    else:
        return false
# driver code
n = 41
# if number is proth number,
# calculate primes upto n
sieveoferatosthenes(n)
for i in range(1, n   1):
    # check n for proth prime
    if isprothprime(i) == true:
        print(i)
# this code is contributed by
# mohit kumar 29

c

// c# implementation of the above approach
using system;
class gfg
{
static boolean[] prime = new boolean[1000000];
// calculate all primes upto n.
static void sieveoferatosthenes(int n)
{
    // initialize all entries it as true.
    // a value in prime[i] will finally
    // false if i is not a prime, else true.
    for (int i = 1; i <= n   1; 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
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i  = p)
                prime[i] = false;
        }
    }
}
// utility function to check power of two
static boolean ispoweroftwo(int n)
{
    return (n > 0 && (n & (n - 1)) == 0);
}
// function to check if the given
// number is proth number or not
static boolean isprothnumber(int n)
{
    int k = 1;
    while (k < (int)(n / k))
    {
        // check if k divides n or not
        if (n % k == 0)
        {
            // check if n/k is power of 2 or not
            if (ispoweroftwo((int)(n / k)))
                return true;
        }
        // update k to next odd number
        k = k   2;
    }
    // if we reach here means there
    // exists no value of k such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
// function to check whether the given
// number is proth prime or not.
static boolean isprothprime(int n)
{
    // check n for proth number
    if (isprothnumber(n - 1))
    {
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
// driver code
static public void main(string []args)
{
    int n = 41;
    // if number is proth number,
    // calculate primes upto n
    sieveoferatosthenes(n);
    for (int i = 1; i <= n; i  )
        // check n for proth prime
        if (isprothprime(i))
            console.writeline(i);
}
}
// this code is contributed by arnab kundu

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

3
5
13
17
41

时间复杂度: o(nlog(log(n))) 参考文献:*

  • https://en . wikipedia . org/wiki/proth _ number # proth _ primes