Given an integer N, the task is to print all safe primes below N safe primes. A safe prime is a prime number of the form (2 * p) + 1 where p is also a prime.
The first few safe primes are 5, 7, 11, 23, 47, …
Input: N = 13
Output: 5 7 11
5 = 2 * 2 + 1
7 = 2 * 3 + 1
11 = 2 * 5 + 1
Input: N = 6
Output: 5 7
Approach: First pre-compute all the primes till N using Sieve of Eratosthenes and then starting from 2 check whether the current prime is also a safe prime. If yes then print it else skip to the next prime.
Below is the implementation of above approach:
5 7 11
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.
- Count of primes below N which can be expressed as the sum of two primes
- Find the sum of all Truncatable primes below N
- Length of largest sub-array having primes strictly greater than non-primes
- Count primes that can be expressed as sum of two consecutive primes and 1
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Sieve of Sundaram to print all primes smaller than n
- Print all Proth primes up to N
- Print all multiplicative primes <= N
- Minimum numbers needed to express every integer below N as a sum
- Find the number of points that have atleast 1 point above, below, left or right of it
- Count of numbers below N whose sum of prime divisors is K
- Sum of the multiples of two numbers below N
- Segmented Sieve (Print Primes in a Range)
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Product of all primes in the range from L to R
- Product of Primes of all Subsets
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Sort all special primes in their relative positions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.