Count prime triplets upto N having sum of first two elements equal to the third number
Last Updated :
01 Dec, 2022
Given an integer N, the task is to find the number of distinct prime triplets from the range [1, N] having the sum of the first two numbers equal to the third number.
Examples:
Input: N = 7
Output: 2
Explanation: All valid triplets are (2, 3, 5) and (2, 5, 7). Therefore, the required output is 2.
Input: N = 4
Output: 0
Approach: The idea is to use Sieve of Eratosthenes. Follow the steps below to solve the problem:
- Initialize an array, say prime[], to mark all the prime numbers up to N using Sieve of Eratosthenes.
- Initialize an array, say cntTriplet[], to store the count of prime triplets upto N which satisfies the above-mentioned condition.
- Traverse the array cntTriplet[] over the indices [3, N] and check for the following conditions:
- If prime[i] and prime[i – 2] are prime numbers, then update the cntTriplet[i] by 1.
- Otherwise, assign cntTriplet[i] = cntTriplet[i – 1].
- Finally, print the value of cntTriplet[N].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000001
bool prime[MAX];
int cntTriplet[MAX];
void primeTriplet( long N)
{
memset (prime, true , sizeof (prime));
prime[0] = prime[1] = false ;
for ( int i = 2; i * i <= N; i++) {
if (prime[i]) {
for ( int j = i * i; j <= N; j += i) {
prime[j] = false ;
}
}
}
for ( int i = 3; i <= N; i++) {
if (prime[i] && prime[i - 2]) {
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else {
cntTriplet[i] = cntTriplet[i - 1];
}
}
cout << cntTriplet[N];
}
int main()
{
long N = 7;
primeTriplet(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int MAX = 1000001 ;
static boolean [] prime = new boolean [MAX];
static int [] cntTriplet = new int [MAX];
static void primeTriplet( int N)
{
Arrays.fill(prime, true );
prime[ 0 ] = prime[ 1 ] = false ;
for ( int i = 2 ; i * i <= N; i++)
{
if (prime[i])
{
for ( int j = i * i; j <= N; j += i)
{
prime[j] = false ;
}
}
}
for ( int i = 3 ; i <= N; i++)
{
if (prime[i] && prime[i - 2 ])
{
cntTriplet[i] = cntTriplet[i - 1 ] + 1 ;
}
else
{
cntTriplet[i] = cntTriplet[i - 1 ];
}
}
System.out.println(cntTriplet[N]);
}
public static void main(String[] args)
{
int N = 7 ;
primeTriplet(N);
}
}
|
Python3
def primeTriplet( N):
prime = [ True for i in range ( 1000001 )]
cntTriplet = [ 0 for i in range ( 1000001 )]
prime[ 0 ] = prime[ 1 ] = False
i = 2
while i * i < = N:
if (prime[i]):
j = i * i
while j < = N:
prime[j] = False
j + = i
i + = 1
for i in range ( 3 , N + 1 ):
if (prime[i] and prime[i - 2 ]):
cntTriplet[i] = cntTriplet[i - 1 ] + 1
else :
cntTriplet[i] = cntTriplet[i - 1 ]
print (cntTriplet[N])
N = 7
primeTriplet(N)
|
C#
using System;
class GFG
{
static int MAX = 1000001;
static bool [] prime = new bool [MAX];
static int [] cntTriplet = new int [MAX];
static void primeTriplet( int N)
{
for ( int i = 0; i <= N; i++)
{
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for ( int i = 2; i * i <= N; i++)
{
if (prime[i])
{
for ( int j = i * i; j <= N; j += i)
{
prime[j] = false ;
}
}
}
for ( int i = 3; i <= N; i++)
{
if (prime[i] && prime[i - 2])
{
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else
{
cntTriplet[i] = cntTriplet[i - 1];
}
}
Console.WriteLine(cntTriplet[N]);
}
public static void Main(String[] args)
{
int N = 7;
primeTriplet(N);
}
}
|
Javascript
<script>
let MAX = 1000001
let prime = new Array(MAX).fill( true );
let cntTriplet = new Array(MAX).fill(0);
function primeTriplet(N)
{
prime[0] = false ;
prime[1] = false ;
for (let i = 2; i * i <= N; i++) {
if (prime[i]) {
for (let j = i * i; j <= N; j += i)
{
prime[j] = false ;
}
}
}
for (let i = 3; i <= N; i++) {
if (prime[i] && prime[i - 2]) {
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else {
cntTriplet[i] = cntTriplet[i - 1];
}
}
document.write(cntTriplet[N]);
}
let N = 7;
primeTriplet(N);
</script>
|
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(MAX)
Share your thoughts in the comments
Please Login to comment...