Related Articles

# Sophie Germain Prime

• Difficulty Level : Medium
• Last Updated : 24 May, 2021

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 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’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 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

 ``

## Javascript

 ``

Output :

`2 3 5 11 23`

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 .

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up