![]() |
VOOZH | about |
In mathematics, Sexy Primes are prime numbers that differ from each other by six. For example, the numbers 5 and 11 are both sexy primes, because they differ by 6. If p + 2 or p + 4 (where p is the lower prime) is also prime.
They can be grouped as:
Given a range of the form [L, R]. The task is to print all the sexy prime pairs in the range.
Examples:
Input : L = 6, R = 59
Output : (7, 13) (11, 17) (13, 19)
(17, 23) (23, 29) (31, 37) (37, 43)
(41, 47) (47, 53) (53, 59)
Input : L = 1, R = 19
Output : (5, 11) (7, 13) (11, 17) (13, 19)
Sexy Prime within a range [L, R] can be generated using Sieve Of Eratosthenes. The idea is to generate bool array of Sieve and run a loop of i from L to R - 6 (inclusive) and check whether i and i + 6 are prime or not. If both are prime, print both number.
Below is the implementation of this approach:
Output:
(7, 13) (11, 17) (13, 19) (17, 23)
(23, 29) (31, 37) (37, 43) (41, 47)
(47, 53) (53, 59)
Time Complexity: O(R*log(log(R)))
Auxiliary Space: O(R)