Skip to content
Related Articles

Related Articles

Sophie Germain Prime
  • Difficulty Level : Medium
  • Last Updated : 01 Aug, 2018
GeeksforGeeks - Summer Carnival Banner

Write a program to print all sophie germain number 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 prime number . The first few Sophie German 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 number below n .
The solution of 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 that whether that number and (2*number + 1), both are prime or not and for checking this we have used Sieve of Erastothenes method.

Below is the implementation of this approach.

C++






// CPP program to print all sophie german
// 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 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
    // 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
# 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 
    // 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




<?php
// PHP program to print 
// all sophie german
// 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 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 = 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
        // german prime or not.
        if ($prime[$i] &&
            $prime[2 * $i + 1]) 
            echo ($i . " ");     
    }
}
  
// Driver code
$n = 25;
printSophieGermanNumber($n);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>


Output :
2 3 5 11 23

Application of Sophie Prime Numbers :

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 .

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :