# Sophie Germain Prime

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 ` `

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 .

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : manishshaw1

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.