Write a program to print all Sophie Germain numbers for less than n. A prime number p is called a Sophie prime number if 2p+1 is also a prime number. The number 2p+1 is called a safe prime. For example, 11 is a prime number and 11*2 + 1 = 23 is also a prime number, so, 11 is Sophie Germain’s prime number. The first few Sophie Germain prime numbers are 2, 3, 5, 11, 23, 29, 41, 53, 83, 89, 113, 131, 173, 179…
Examples:
Input : 25 Output : 2 3 5 11 23
Here is the program to print Sophie Germain’s number below n.
The solution to this is simple. To obtain all the Sophie numbers below n we will make a loop till n and for each number in the loop we can check, whether that number and (2*number + 1), are both prime or not, and for checking this we have used Sieve of Eratosthenes method.
Below is the implementation of this approach.
C++
// CPP program to print all sophie germain // prime number till n. #include <bits/stdc++.h> using namespace std; // function to detect prime number // here we have used sieve method // 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 printSophieGermainNumber( int n) { // We have made array till 2*n +1 // so that we can check prime number // till that and conclude about sophie // germain 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 germain prime or not. if (prime[i] && prime[2 * i + 1]) cout << i << " " ; } } int main() { int n = 25; printSophieGermainNumber(n); return 0; } |
Java
// Java program to print all // sophie germain prime number till n. import java.io.*; import java.util.*; class GFG { // function to detect prime number // here we have used sieve method // 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 printSophieGermainNumber( int n) { // We have made array till 2*n +1 // so that we can check prime number // till that and conclude about sophie // germain 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 germain prime or not. if (prime[i] && prime[ 2 * i + 1 ]) System.out.print( i + " " ); } } public static void main(String args[]) { int n = 25 ; printSophieGermainNumber(n); } } // This code is contributed // by Nikita Tiwari. |
Python3
# Python 3 program to print all sophie # germain prime number till n. # Function to detect prime number # here we have used sieve method # 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 printSophieGermainNumber(n) : # We have made array till 2*n +1 # so that we can check prime number # till that and conclude about sophie # germain 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 germain prime or not. if (prime[i] and prime[ 2 * i + 1 ]) : print ( i , end = " " ) # Driver Code n = 25 printSophieGermainNumber(n) # This code is contributed by Nikita Tiwari. |
C#
// C# program to print // all sophie germain // prime number till n. using System; class GFG { // function to detect prime // number here we have used // sieve method // 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 printSophieGermainNumber( int n) { // We have made array till // 2*n +1 so that we can // check prime number till // that and conclude about // sophie germain 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 germain prime // or not. if (prime[i] && prime[2 * i + 1]) Console.Write( i + " " ); } } // Driver code static void Main() { int n = 25; printSophieGermainNumber(n); } } // This code is contributed by // Manish Shaw(manishshaw1) |
PHP
<?php // PHP program to print // all sophie germain // prime number till n. // function to detect prime // number here we have used // sieve method // to detect prime number function sieve( $n , & $prime ) { for ( $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 ( $i = $p * 2; $i <= $n ; $i += $p ) $prime [ $i ] = false; } } } function printSophieGermainNumber( $n ) { // We have made array till // 2*n +1 so that we can // check prime number till // that and conclude about // sophie germain prime . $prime = array (); for ( $i = 0; $i < (2 * $n + 1); $i ++) $prime [ $i ] = true; sieve(2 * $n + 1, $prime ); for ( $i = 2; $i <= $n ; ++ $i ) { // checking every i // whether it is sophie // germain prime or not. if ( $prime [ $i ] && $prime [2 * $i + 1]) echo ( $i . " " ); } } // Driver code $n = 25; printSophieGermainNumber( $n ); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Javascript
<script> // Javascript program to print // all sophie germain // prime number till n. // function to detect prime // number here we have used // sieve method // to detect prime number function sieve(n, prime) { for (let 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 (let i = p * 2; i <= n; i += p) prime[i] = false ; } } } function printSophieGermainNumber(n) { // We have made array till // 2*n +1 so that we can // check prime number till // that and conclude about // sophie germain prime . let prime = new Array(); for (let i = 0; i < (2 * n + 1); i++) prime[i] = true ; sieve(2 * n + 1, prime); for (let i = 2; i <= n; ++i) { // checking every i // whether it is sophie // germain prime or not. if (prime[i] && prime[2 * i + 1]) document.write(i + " " ); } } // Driver code let n = 25; printSophieGermainNumber(n); // This code is contributed by // gfgking </script> |
Output :
2 3 5 11 23
Time Complexity: O(nloglogn)
Auxiliary Space: O(1)
Application of Sophie Prime Numbers :
1. It is used in cryptography as safe primes become factors of a secret key in RSA cryptosystem.
2. In the first version of the AKS Primality Test, it is used to lower the worst case complexity .
3. It is used in the generation of Pseudo Random Number .
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!