Given two integers l and r representing a range [l, r], find all Sixy prime pairs within the range. Two prime numbers are called Sixy primes if their difference is exactly 6. Â
Return all such pairs in increasing order of the first prime. For every valid pair (p, p + 6), append p followed by p + 6 to the result.  If no such pair exists, return an empty list.Â
Examples:Â
Input: l = 11, r = 19
Output:Â [11, 17, 13, 19]Â
Explanation: There are total two pair possible with difference 6 and these are 11,17,13,19.Input: l = 6, r = 20
Output:Â [7, 13, 11, 17, 13, 19]
Explanation: There are total three pair possible with difference 6 and these are 7,13,11,17,13,19.
Table of Content
[Naive Approach] By Checking Every Pair - O((r - l + 1) * sqrt(r)) Time and O(1) Space
The idea is to simply iterate through every number from l to r - 6 and check whether both i and i + 6 are prime. If both are prime, then (i, i + 6) is a valid Sixy prime pair.
- Create an empty result vector.
- Traverse every number i from l to r - 6.
- Check if i and i + 6 are prime.
- If both are prime, append i and i + 6 to the result.
#include <bits/stdc++.h>
using namespace std;
// Returns true if n is a prime number.
bool isPrime(int n)
{
// Numbers less than 2 are not prime.
if (n < 2)
return false;
// Check for factors from 2 to sqrt(n).
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Returns all Sixy prime pairs in the range [l, r].
vector<int> sixyPrime(int l, int r)
{
vector<int> res;
// Check every possible pair (i, i + 6).
for (int i = l; i <= r - 6; i++)
{
// If both numbers are prime, store the pair.
if (isPrime(i) && isPrime(i + 6))
{
res.push_back(i);
res.push_back(i + 6);
}
}
return res;
}
int main()
{
int l = 11, r = 19;
vector<int> ans = sixyPrime(l, r);
for (int i = 0; i < ans.size(); i += 1)
cout << ans[i] << " ";
return 0;
}
import java.util.*;
class GFG {
// Returns true if n is a prime number.
static boolean isPrime(int n)
{
// Numbers less than 2 are not prime.
if (n < 2)
return false;
// Check for factors from 2 to sqrt(n).
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Returns all Sixy prime pairs in the range [l, r].
static ArrayList<Integer> sixyPrime(int l, int r)
{
ArrayList<Integer> res = new ArrayList<>();
// Check every possible pair (i, i + 6).
for (int i = l; i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (isPrime(i) && isPrime(i + 6)) {
res.add(i);
res.add(i + 6);
}
}
return res;
}
public static void main(String[] args)
{
int l = 11, r = 19;
ArrayList<Integer> ans = sixyPrime(l, r);
for (int x : ans)
System.out.print(x + " ");
}
}
def isPrime(n):
# Numbers less than 2 are not prime.
if n < 2:
return False
# Check for factors from 2 to sqrt(n).
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
# Returns all Sixy prime pairs in the range [l, r].
def sixyPrime(l, r):
res = []
# Check every possible pair (i, i + 6).
for i in range(l, r - 5):
# If both numbers are prime, store the pair.
if isPrime(i) and isPrime(i + 6):
res.append(i)
res.append(i + 6)
return res
# Driver Code
if __name__ == "__main__":
l = 11
r = 19
ans = sixyPrime(l, r)
for x in ans:
print(x, end=" ")
using System;
using System.Collections.Generic;
class GFG {
// Returns true if n is a prime number.
static bool IsPrime(int n)
{
// Numbers less than 2 are not prime.
if (n < 2)
return false;
// Check for factors from 2 to sqrt(n).
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Returns all Sixy prime pairs in the range [l, r].
static List<int> sixyPrime(int l, int r)
{
List<int> res = new List<int>();
// Check every possible pair (i, i + 6).
for (int i = l; i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (IsPrime(i) && IsPrime(i + 6)) {
res.Add(i);
res.Add(i + 6);
}
}
return res;
}
static void Main()
{
int l = 11, r = 19;
List<int> ans = sixyPrime(l, r);
foreach(int x in ans) Console.Write(x + " ");
}
}
// Returns true if n is a prime number.
function isPrime(n)
{
// Numbers less than 2 are not prime.
if (n < 2)
return false;
// Check for factors from 2 to sqrt(n).
for (let i = 2; i * i <= n; i++) {
if (n % i === 0)
return false;
}
return true;
}
// Returns all Sixy prime pairs in the range [l, r].
function sixyPrime(l, r)
{
let res = [];
// Check every possible pair (i, i + 6).
for (let i = l; i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (isPrime(i) && isPrime(i + 6)) {
res.push(i);
res.push(i + 6);
}
}
return res;
}
// Driver Code
let l = 11;
let r = 19;
let ans = sixyPrime(l, r);
for (let x of ans)
process.stdout.write(x + " ");
Output
11 17 13 19
[Expected Approach] Using Sieve of Eratosthenes - O(r log log r) Time and O(r) Space
We can check primality of every number from 2 to r in one preprocessing step using the Sieve of Eratosthenes. Once the sieve is built, checking whether a number is prime takes O(1) time. We then simply traverse the range [l, r - 6] and collect every pair (i, i + 6) where both numbers are prime.
- Create a boolean array prime of size r + 1 and initialize all entries as true.
- Mark 0 and 1 as non-prime.
- Traverse from 2 to sqrt(r).
- If the current number is prime, mark all of its multiples starting from i * i as non-prime.
- Create an empty result vector. Traverse from max(l, 2) to r - 6.
- If both prime[i] and prime[i + 6] are true, append i and i + 6 to the result. Return the result.
#include <bits/stdc++.h>
using namespace std;
// Returns all Sixy prime pairs in the range [l, r].
vector<int> sixyPrime(int l, int r)
{
vector<int> res;
// There are no prime numbers less than 2.
if (r < 2)
return res;
// Stores whether each number is prime.
vector<bool> prime(r + 1, true);
// Mark 0 and 1 as non-prime.
prime[0] = false;
prime[1] = false;
// Generate all prime numbers up to r using Sieve of Eratosthenes.
for (int i = 2; i * i <= r; i++)
{
if (prime[i])
{
// Mark all multiples of i as non-prime.
for (int j = i * i; j <= r; j += i)
prime[j] = false;
}
}
// Check every possible pair (i, i + 6).
for (int i = max(l, 2); i <= r - 6; i++)
{
// If both numbers are prime, store the pair.
if (prime[i] && prime[i + 6])
{
res.push_back(i);
res.push_back(i + 6);
}
}
return res;
}
int main()
{
int l = 11, r = 19;
vector<int> ans = sixyPrime(l, r);
for (int x : ans)
cout << x << " ";
return 0;
}
import java.util.*;
class GFG {
static ArrayList<Integer> sixyPrime(int l, int r)
{
ArrayList<Integer> res = new ArrayList<>();
// There are no prime numbers less than 2.
if (r < 2)
return res;
// Stores whether each number is prime.
boolean[] prime = new boolean[r + 1];
Arrays.fill(prime, true);
// Mark 0 and 1 as non-prime.
prime[0] = false;
prime[1] = false;
// Generate all prime numbers up to r using Sieve of
// Eratosthenes.
for (int i = 2; i * i <= r; i++) {
if (prime[i]) {
// Mark all multiples of i as non-prime.
for (int j = i * i; j <= r; j += i)
prime[j] = false;
}
}
// Check every possible pair (i, i + 6).
for (int i = Math.max(l, 2); i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (prime[i] && prime[i + 6]) {
res.add(i);
res.add(i + 6);
}
}
return res;
}
public static void main(String[] args)
{
int l = 11, r = 19;
ArrayList<Integer> ans = sixyPrime(l, r);
for (int x : ans)
System.out.print(x + " ");
}
}
# Returns all Sixy prime pairs in the range [l, r].
def sixyPrime(l, r):
res = []
# There are no prime numbers less than 2.
if r < 2:
return res
# Stores whether each number is prime.
prime = [True] * (r + 1)
# Mark 0 and 1 as non-prime.
prime[0] = False
prime[1] = False
# Generate all prime numbers up to r using Sieve of Eratosthenes.
i = 2
while i * i <= r:
if prime[i]:
# Mark all multiples of i as non-prime.
j = i * i
while j <= r:
prime[j] = False
j += i
i += 1
# Check every possible pair (i, i + 6).
for i in range(max(l, 2), r - 5):
# If both numbers are prime, store the pair.
if prime[i] and prime[i + 6]:
res.append(i)
res.append(i + 6)
return res
# Driver Code
if __name__ == "__main__":
l = 11
r = 19
ans = sixyPrime(l, r)
for x in ans:
print(x, end=" ")
using System;
using System.Collections.Generic;
class GFG {
// Returns all Sixy prime pairs in the range [l, r].
static List<int> sixyPrime(int l, int r)
{
List<int> res = new List<int>();
// There are no prime numbers less than 2.
if (r < 2)
return res;
// Stores whether each number is prime.
bool[] prime = new bool[r + 1];
Array.Fill(prime, true);
// Mark 0 and 1 as non-prime.
prime[0] = false;
prime[1] = false;
// Generate all prime numbers up to r using Sieve of
// Eratosthenes.
for (int i = 2; i * i <= r; i++) {
if (prime[i]) {
// Mark all multiples of i as non-prime.
for (int j = i * i; j <= r; j += i)
prime[j] = false;
}
}
// Check every possible pair (i, i + 6).
for (int i = Math.Max(l, 2); i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (prime[i] && prime[i + 6]) {
res.Add(i);
res.Add(i + 6);
}
}
return res;
}
static void Main()
{
int l = 11, r = 19;
List<int> ans = sixyPrime(l, r);
foreach(int x in ans) Console.Write(x + " ");
}
}
// Returns all Sixy prime pairs in the range [l, r].
function sixyPrime(l, r)
{
let res = [];
// There are no prime numbers less than 2.
if (r < 2)
return res;
// Stores whether each number is prime.
let prime = new Array(r + 1).fill(true);
// Mark 0 and 1 as non-prime.
prime[0] = false;
prime[1] = false;
// Generate all prime numbers up to r using Sieve of
// Eratosthenes.
for (let i = 2; i * i <= r; i++) {
if (prime[i]) {
// Mark all multiples of i as non-prime.
for (let j = i * i; j <= r; j += i)
prime[j] = false;
}
}
// Check every possible pair (i, i + 6).
for (let i = Math.max(l, 2); i <= r - 6; i++) {
// If both numbers are prime, store the pair.
if (prime[i] && prime[i + 6]) {
res.push(i);
res.push(i + 6);
}
}
return res;
}
// Driver Code
let l = 11;
let r = 19;
let ans = sixyPrime(l, r);
for (let x of ans)
process.stdout.write(x + " ");
Output
11 17 13 19