Print safe prime numbers from 1 to n

Here given code implementation process.

// Java program for
// Print safe prime numbers from 1 to n
public class PrimeNumber
{
	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)
	{
		PrimeNumber task = new PrimeNumber();
		// Test
		task.printSafePrime(100);
		task.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
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Print safe prime numbers from 1 to n
class PrimeNumber
{
	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()
{
	PrimeNumber *task = new PrimeNumber();
	// Test
	task->printSafePrime(100);
	task->printSafePrime(300);
	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 class PrimeNumber
{
	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)
	{
		PrimeNumber task = new PrimeNumber();
		// Test
		task.printSafePrime(100);
		task.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
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
class PrimeNumber
{
	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()
{
	$task = new PrimeNumber();
	// Test
	$task->printSafePrime(100);
	$task->printSafePrime(300);
}
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
class PrimeNumber
{
	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()
{
	var task = new PrimeNumber();
	// Test
	task.printSafePrime(100);
	task.printSafePrime(300);
}
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
class PrimeNumber :
	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() :
	task = PrimeNumber()
	#  Test
	task.printSafePrime(100)
	task.printSafePrime(300)

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
class PrimeNumber 
	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() 
	task = PrimeNumber.new()
	#  Test
	task.printSafePrime(100)
	task.printSafePrime(300)
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
class PrimeNumber()
{
	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 = {
		var task: PrimeNumber = new PrimeNumber();
		// Test
		task.printSafePrime(100);
		task.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
// Swift 4 program for
// Print safe prime numbers from 1 to n
class PrimeNumber
{
	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()
{
	let task: PrimeNumber = PrimeNumber();
	// Test
	task.printSafePrime(100);
	task.printSafePrime(300);
}
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
class PrimeNumber
{
	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
{
	val task: PrimeNumber = PrimeNumber();
	// Test
	task.printSafePrime(100);
	task.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


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.

New Comment







© 2021, kalkicode.com, All rights reserved