# Print safe prime numbers from 1 to n

A safe prime number is a prime number of the form 2p+1, where both p and (2p+1) are prime numbers.

To print safe prime numbers from 1 to n, you would need to perform the following steps:

1. Create a loop that iterates from 2 to n, testing each number to determine if it is a prime number.

2. For each prime number found in step 1, test if 2p+1 (where p is the prime number) is also a prime number.

3. If 2p+1 is a prime number, then the number is a safe prime. Print it.

## Program Solution

``````// Java program for
// Print safe prime numbers from 1 to n
{
public void eratosthenesSieve(boolean[] prime, int n)
{
// Set all element as prime
for (int i = 0; i <= n; ++i)
{
prime[i] = true;
}
prime[0] = false;
prime[1] = false;
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
for (int j = i *i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
public void printSafePrime(int n)
{
if (n < 1)
{
return;
}
System.out.println("\n  Given n :" + n);
boolean[] prime = new boolean[n + 1];
eratosthenesSieve(prime, n);
int j = 0;
// Print safe prime (2..n)
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
j = (i * 2) + 1;
if (j <= n && prime[j])
{
System.out.print("  " + j);
}
}
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Print safe prime numbers from 1 to n
{
public: void eratosthenesSieve(bool prime[], int n)
{
// Set all element as prime
for (int i = 0; i <= n; ++i)
{
prime[i] = true;
}
prime[0] = false;
prime[1] = false;
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
for (int j = i *i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
void printSafePrime(int n)
{
if (n < 1)
{
return;
}
cout << "\n  Given n :"
<< n << endl;
bool prime[n + 1];
this->eratosthenesSieve(prime, n);
int j = 0;
// Print safe prime (2..n)
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
j = (i *2) + 1;
if (j <= n && prime[j])
{
cout << "  " << j;
}
}
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````// Include namespace system
using System;
// Csharp program for
// Print safe prime numbers from 1 to n
{
public void eratosthenesSieve(Boolean[] prime, int n)
{
// Set all element as prime
for (int i = 0; i <= n; ++i)
{
prime[i] = true;
}
prime[0] = false;
prime[1] = false;
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
for (int j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
public void printSafePrime(int n)
{
if (n < 1)
{
return;
}
Console.WriteLine("\n  Given n :" + n);
Boolean[] prime = new Boolean[n + 1];
this.eratosthenesSieve(prime, n);
int j = 0;
// Print safe prime (2..n)
for (int i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
j = (i * 2) + 1;
if (j <= n && prime[j])
{
Console.Write("  " + j);
}
}
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````package main
import "fmt"
// Go program for
// Print safe prime numbers from 1 to n

func eratosthenesSieve(prime[] bool, n int) {
// Set all element as prime
for i := 0 ; i <= n ; i++ {
prime[i] = true
}
prime[0] = false
prime[1] = false
for i := 2 ; i <= n ; i++ {
if prime[i] == true {
for j := i * i ; j <= n ; j += i {
prime[j] = false
}
}
}
}
func printSafePrime(n int) {
if n < 1 {
return
}
fmt.Println("\n  Given n :", n)
var prime = make([] bool, n + 1)
eratosthenesSieve(prime, n)
var j int = 0
// Print safe prime (2..n)
for i := 2 ; i <= n ; i++ {
if prime[i] == true {
j = (i * 2) + 1
if j <= n && prime[j] {
fmt.Print("  ", j)
}
}
}
}
func main() {

// Test
printSafePrime(100)
printSafePrime(300)
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````<?php
// Php program for
// Print safe prime numbers from 1 to n
{
public	function eratosthenesSieve(&\$prime, \$n)
{
\$prime[0] = false;
\$prime[1] = false;
for (\$i = 2; \$i <= \$n; ++\$i)
{
if (\$prime[\$i] == true)
{
for (\$j = \$i * \$i; \$j <= \$n; \$j += \$i)
{
\$prime[\$j] = false;
}
}
}
}
public	function printSafePrime(\$n)
{
if (\$n < 1)
{
return;
}
echo("\n  Given n :".\$n."\n");
// Set all element as prime
\$prime = array_fill(0, \$n + 1, true);
\$this->eratosthenesSieve(\$prime, \$n);
\$j = 0;
// Print safe prime (2..n)
for (\$i = 2; \$i <= \$n; ++\$i)
{
if (\$prime[\$i] == true)
{
\$j = (\$i * 2) + 1;
if (\$j <= \$n && \$prime[\$j])
{
echo("  ".\$j);
}
}
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````// Node JS program for
// Print safe prime numbers from 1 to n
{
eratosthenesSieve(prime, n)
{
prime[0] = false;
prime[1] = false;
for (var i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
for (var j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
printSafePrime(n)
{
if (n < 1)
{
return;
}
console.log("\n  Given n : " + n);
// Set all element as prime
var prime = Array(n + 1).fill(true);
this.eratosthenesSieve(prime, n);
var j = 0;
// Print safe prime (2..n)
for (var i = 2; i <= n; ++i)
{
if (prime[i] == true)
{
j = (i * 2) + 1;
if (j <= n && prime[j])
{
process.stdout.write("  " + j);
}
}
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````  Given n : 100
5  7  11  23  47  59  83
Given n : 300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````#  Python 3 program for
#  Print safe prime numbers from 1 to n
def eratosthenesSieve(self, prime, n) :
prime[0] = False
prime[1] = False
i = 2
while (i <= n) :
if (prime[i] == True) :
j = i * i
while (j <= n) :
prime[j] = False
j += i

i += 1

def printSafePrime(self, n) :
if (n < 1) :
return

print("\n  Given n :", n)
#  Set all element as prime
prime = [True] * (n + 1)
self.eratosthenesSieve(prime, n)
j = 0
i = 2
#  Print safe prime (2..n)
while (i <= n) :
if (prime[i] == True) :
j = (i * 2) + 1
if (j <= n and prime[j]) :
print("  ", j, end = "")

i += 1

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

``````  Given n : 100
5   7   11   23   47   59   83
Given n : 300
5   7   11   23   47   59   83   107   167   179   227   263``````
``````#  Ruby program for
#  Print safe prime numbers from 1 to n
def eratosthenesSieve(prime, n)
prime[0] = false
prime[1] = false
i = 2
while (i <= n)
if (prime[i] == true)
j = i * i
while (j <= n)
prime[j] = false
j += i
end

end

i += 1
end

end

def printSafePrime(n)
if (n < 1)
return
end

print("\n  Given n :", n, "\n")
#  Set all element as prime
prime = Array.new(n + 1) {true}
self.eratosthenesSieve(prime, n)
j = 0
i = 2
#  Print safe prime (2..n)
while (i <= n)
if (prime[i] == true)
j = (i * 2) + 1
if (j <= n && prime[j])
print("  ", j)
end

end

i += 1
end

end

end

def main()
#  Test
end

main()``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````// Scala program for
// Print safe prime numbers from 1 to n
{
def eratosthenesSieve(prime: Array[Boolean], n: Int): Unit = {
prime(0) = false;
prime(1) = false;
var i: Int = 2;
while (i <= n)
{
if (prime(i) == true)
{
var j: Int = i * i;
while (j <= n)
{
prime(j) = false;
j += i;
}
}
i += 1;
}
}
def printSafePrime(n: Int): Unit = {
if (n < 1)
{
return;
}
println("\n  Given n :" + n);
// Set all element as prime
var prime: Array[Boolean] = Array.fill[Boolean](n + 1)(true);
eratosthenesSieve(prime, n);
var j: Int = 0;
var i: Int = 2;
// Print safe prime (2..n)
while (i <= n)
{
if (prime(i) == true)
{
j = (i * 2) + 1;
if (j <= n && prime(j))
{
print("  " + j);
}
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Test
}
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````
``````// Swift 4 program for
// Print safe prime numbers from 1 to n
{
func eratosthenesSieve(_ prime: inout[Bool], _ n: Int)
{
prime[0] = false;
prime[1] = false;
var i: Int = 2;
while (i <= n)
{
if (prime[i] == true)
{
var j: Int = i * i;
while (j <= n)
{
prime[j] = false;
j += i;
}
}
i += 1;
}
}
func printSafePrime(_ n: Int)
{
if (n < 1)
{
return;
}
print("\n  Given n :", n);
// Set all element as prime
var prime: [Bool] = Array(repeating: true, count: n + 1);
self.eratosthenesSieve(&prime, n);
var j: Int = 0;
var i: Int = 2;
// Print safe prime (2..n)
while (i <= n)
{
if (prime[i] == true)
{
j = (i * 2) + 1;
if (j <= n && prime[j])
{
print("  ", j, terminator: "");
}
}
i += 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

``````  Given n : 100
5   7   11   23   47   59   83
Given n : 300
5   7   11   23   47   59   83   107   167   179   227   263``````
``````// Kotlin program for
// Print safe prime numbers from 1 to n
{
fun eratosthenesSieve(prime: Array < Boolean > , n: Int): Unit
{
prime[0] = false;
prime[1] = false;
var i: Int = 2;
while (i <= n)
{
if (prime[i] == true)
{
var j: Int = i * i;
while (j <= n)
{
prime[j] = false;
j += i;
}
}
i += 1;
}
}
fun printSafePrime(n: Int): Unit
{
if (n < 1)
{
return;
}
println("\n  Given n :" + n);
// Set all element as prime
var prime: Array < Boolean > = Array(n + 1)
{
true
};
this.eratosthenesSieve(prime, n);
var j: Int;
var i: Int = 2;
// Print safe prime (2..n)
while (i <= n)
{
if (prime[i] == true)
{
j = (i * 2) + 1;
if (j <= n && prime[j])
{
print("  " + j);
}
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

``````  Given n :100
5  7  11  23  47  59  83
Given n :300
5  7  11  23  47  59  83  107  167  179  227  263``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.