Count distinct prime triplets up to N such that sum of two primes is equal to the third prime
Given an integer N, the task is to count the number of distinct prime triplets (a, b, c) from the range [1, N] such that a < b < c ? N and a + b = c.
Note: Two prime tuples are distinct if at least one of the primes present in them are different.
Examples:
Input: N = 6
Output: 1
Explanation: Among numbers in the range [1, 6], the only prime triplet is (2, 3, 5) (Since 2 + 3 = 5).
Input: N = 10
Output: 2
Explanation: The distinct prime triplets satisfying the condition are (2, 3, 5), (2, 5, 7).
Approach: The problem can be solved based on the observation stated below:
Observation:
For every prime number p from 1 to N, it is a part of a triplet if and only if it can be represented as a sum of two prime numbers.
Since a prime number is an odd number, it must be equal to the sum of an even number and an odd number.
Hence the only even prime is 2. Therefore, for a prime number p to constitute a unique tuple (2, p-2, p), the number p – 2 must be a prime number.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int N)
{
if (N <= 1)
return false ;
for ( int i = 2; i <= sqrt (N); i++) {
if (N % i == 0)
return false ;
}
return true ;
}
void countPrimeTuples( int N)
{
int count = 0;
for ( int i = 2; i <= N; i++) {
if (isPrime(i) && isPrime(i - 2))
count++;
}
cout << count;
}
int main()
{
int N = 6;
countPrimeTuples(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isPrime( int N)
{
if (N <= 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(N); i++)
{
if (N % i == 0 )
return false ;
}
return true ;
}
static void countPrimeTuples( int N)
{
int count = 0 ;
for ( int i = 2 ; i <= N; i++)
{
if (isPrime(i) && isPrime(i - 2 ))
count++;
}
System.out.println(count);
}
public static void main (String[] args)
{
int N = 6 ;
countPrimeTuples(N);
}
}
|
Python3
import math
def isPrime(N) :
if (N < = 1 ) :
return False
for i in range ( 2 , int (math.sqrt(N) + 1 )):
if (N % i = = 0 ) :
return False
return True
def countPrimeTuples(N) :
count = 0
for i in range ( 2 , N + 1 ):
if (isPrime(i) and isPrime(i - 2 )) :
count + = 1
print (count)
N = 6
countPrimeTuples(N)
|
C#
using System;
public class GFG
{
static bool isPrime( int N)
{
if (N <= 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(N); i++)
{
if (N % i == 0)
return false ;
}
return true ;
}
static void countPrimeTuples( int N)
{
int count = 0;
for ( int i = 2; i <= N; i++)
{
if (isPrime(i) && isPrime(i - 2))
count++;
}
Console.WriteLine(count);
}
static public void Main ()
{
int N = 6;
countPrimeTuples(N);
}
}
|
Javascript
<script>
function isPrime(N)
{
if (N <= 1)
return false ;
for (let i = 2; i <= Math.sqrt(N); i++) {
if (N % i == 0)
return false ;
}
return true ;
}
function countPrimeTuples(N)
{
let count = 0;
for (let i = 2; i <= N; i++) {
if (isPrime(i) && isPrime(i - 2))
count++;
}
document.write(count);
}
let N = 6;
countPrimeTuples(N);
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...