编写程序以打印所有小于n的sophie germain数。如果2p + 1也是素数,则素数p称为sophie素数。数字2p + 1被称为安全素数。例如11是素数,而11 * 2 +1 = 23也是素数,因此11是sophie germain素数。前几个索菲德文素数是2、3、5、11、23、29、41、53、83、89、113、131、173、179 ..
例子:
Input : 25
Output : 2 3 5 11 23
这是在n以下打印sophie germain编号的程序。
解决的方法很简单。为了获得n以下的所有sophie数,我们将进行循环直到n,对于循环中的每个数字,我们可以检查该数字和(2 * number + 1)两者是否都是质数,并且为了检查该数,我们使用了筛网方法。
以下是此方法的实现。
C++
// CPP program to print all sophie german
// prime number till n.
#include
using namespace std;
// function to detect prime number
// here we have used sieve method
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
// to detect prime number
bool sieve(int n, bool 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;
}
}
}
void printSophieGermanNumber(int n)
{
// We have made array till 2*n +1
// so that we can check prime number
// till that and conclude about sophie
// german prime .
bool prime[2 * n + 1];
memset(prime, true, sizeof(prime));
sieve(2 * n + 1, prime);
for (int i = 2; i <= n; ++i) {
// checking every i whether it is
// sophie german prime or not.
if (prime[i] && prime[2 * i + 1])
cout << i << " ";
}
}
int main()
{
int n = 25;
printSophieGermanNumber(n);
return 0;
}
Java
// Java program to print all
// sophie german prime number till n.
import java.io.*;
import java.util.*;
class GFG {
// function to detect prime number
// here we have used sieve method
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
// to detect prime number
static void sieve(int n, boolean 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;
}
}
}
static void printSophieGermanNumber(int n)
{
// We have made array till 2*n +1
// so that we can check prime number
// till that and conclude about sophie
// german prime .
boolean prime[]=new boolean[2 * n + 1];
Arrays.fill(prime,true);
sieve(2 * n + 1, prime);
for (int i = 2; i < n; ++i) {
// checking every i whether it is
// sophie german prime or not.
if (prime[i] && prime[2 * i + 1])
System.out.print( i + " ");
}
}
public static void main(String args[])
{
int n = 25;
printSophieGermanNumber(n);
}
}
// This code is contributed
// by Nikita Tiwari.
Python3
# Python 3 program to print all sophie
# german prime number till n.
# Function to detect prime number
# here we have used sieve method
# https://www.geeksforgeeks.org/sieve-of-eratosthenes/
# to detect prime number
def sieve(n, prime) :
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, p) :
prime[i] = False
p += 1
def printSophieGermanNumber(n) :
# We have made array till 2*n +1
# so that we can check prime number
# till that and conclude about sophie
# german prime .
prime = [True]*(2 * n + 1)
sieve(2 * n + 1, prime)
for i in range(2, n + 1) :
# checking every i whether it is
# sophie german prime or not.
if (prime[i] and prime[2 * i + 1]) :
print( i , end = " ")
# Driver Code
n = 25
printSophieGermanNumber(n)
# This code is contributed by Nikita Tiwari.
C#
// C# program to print
// all sophie german
// prime number till n.
using System;
class GFG
{
// function to detect prime
// number here we have used
// sieve method
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
// to detect prime number
static void sieve(int n,
bool []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;
}
}
}
static void printSophieGermanNumber(int n)
{
// We have made array till
// 2*n +1 so that we can
// check prime number till
// that and conclude about
// sophie german prime .
bool []prime = new bool[2 * n + 1];
for (int i = 0;
i < prime.Length; i++)
{
prime[i] = true;
}
sieve(2 * n + 1, prime);
for (int i = 2; i < n; ++i)
{
// checking every i whether
// it is sophie german prime
// or not.
if (prime[i] && prime[2 * i + 1])
Console.Write( i + " ");
}
}
// Driver code
static void Main()
{
int n = 25;
printSophieGermanNumber(n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
PHP
输出 :
2 3 5 11 23
苏菲素数的应用:
1. It is used in cryptography as safe primes become the factors of a secret key in RSA cryptosystem.
2. In the first version of AKS Primality Test, it is used to lower the worst case complexity .
3. It is used in the generation of Pseudo Random Number .