Related Articles
Count prime triplets upto N having sum of first two elements equal to the third number
• Difficulty Level : Medium
• Last Updated : 11 May, 2021

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;``#define MAX 1000001` `// Boolean array to``// mark prime numbers``bool` `prime[MAX];` `// To count the prime triplets``// having the sum of the first two``// numbers equal to the third element``int` `cntTriplet[MAX];` `// Function to count prime triplets``// having sum of the first two elements``// equal to the third element``void` `primeTriplet(``long` `N)``{``    ``// Sieve of Eratosthenes``    ``memset``(prime, ``true``, ``sizeof``(prime));` `    ``prime = prime = ``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++) {` `        ``// Checks for the prime numbers``        ``// having difference equal to2``        ``if` `(prime[i] && prime[i - 2]) {` `            ``// Update count of triplets``            ``cntTriplet[i] = cntTriplet[i - 1] + 1;``        ``}``        ``else` `{` `            ``cntTriplet[i] = cntTriplet[i - 1];``        ``}``    ``}` `    ``// Print the answer``    ``cout << cntTriplet[N];``}` `// Driver Code``int` `main()``{``    ``long` `N = 7;``    ``primeTriplet(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG{``static` `int` `MAX = ``1000001``;` `// Boolean array to``// mark prime numbers``static` `boolean``[] prime = ``new` `boolean``[MAX];` `// To count the prime triplets``// having the sum of the first two``// numbers equal to the third element``static` `int``[] cntTriplet = ``new` `int``[MAX];` `// Function to count prime triplets``// having sum of the first two elements``// equal to the third element``static` `void` `primeTriplet(``int` `N)``{``    ``// Sieve of Eratosthenes``    ``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++)``    ``{` `        ``// Checks for the prime numbers``        ``// having difference equal to2``        ``if` `(prime[i] && prime[i - ``2``])``        ``{` `            ``// Update count of triplets``            ``cntTriplet[i] = cntTriplet[i - ``1``] + ``1``;``        ``}``        ``else``        ``{``            ``cntTriplet[i] = cntTriplet[i - ``1``];``        ``}``    ``}` `    ``// Print the answer``    ``System.out.println(cntTriplet[N]);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``7``;``    ``primeTriplet(N);``}``}` `// This code is contributed by susmitakundugoaldagna.`

## Python3

 `# Python program for the above approach` `# Function to count prime triplets``# having sum of the first two elements``# equal to the third element``def` `primeTriplet( N):``  ` `    ``# Sieve of Eratosthenes   ``    ``# Boolean array to``    ``# mark prime numbers``    ``prime ``=` `[``True` `for` `i ``in` `range``(``1000001``)]` `    ``# To count the prime triplets``    ``# having the sum of the first two``    ``# numbers equal to the third element``    ``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``):``      ` `        ``# Checks for the prime numbers``        ``# having difference equal to2``        ``if` `(prime[i] ``and` `prime[i ``-` `2``]):``          ` `            ``# Update count of triplets``            ``cntTriplet[i] ``=` `cntTriplet[i ``-` `1``] ``+` `1``        ``else``:``            ``cntTriplet[i] ``=` `cntTriplet[i ``-` `1``]` `    ``# Print the answer``    ``print``(cntTriplet[N])` `# Driver Code``N ``=` `7``primeTriplet(N)` `# This code is contributed by rohitsingh07052`

## C#

 `// C# Program to implement``// the above approach``using` `System;``class` `GFG``{``static` `int` `MAX = 1000001;`` ` `// Boolean array to``// mark prime numbers``static` `bool``[] prime = ``new` `bool``[MAX];`` ` `// To count the prime triplets``// having the sum of the first two``// numbers equal to the third element``static` `int``[] cntTriplet = ``new` `int``[MAX];`` ` `// Function to count prime triplets``// having sum of the first two elements``// equal to the third element``static` `void` `primeTriplet(``int` `N)``{``    ``// Sieve of Eratosthenes``    ``for``(``int` `i = 0; i <= N; i++)``    ``{``        ``prime[i] = ``true``;``    ``}``    ``prime = prime = ``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++)``    ``{`` ` `        ``// Checks for the prime numbers``        ``// having difference equal to2``        ``if` `(prime[i] && prime[i - 2])``        ``{`` ` `            ``// Update count of triplets``            ``cntTriplet[i] = cntTriplet[i - 1] + 1;``        ``}``        ``else``        ``{``            ``cntTriplet[i] = cntTriplet[i - 1];``        ``}``    ``}`` ` `    ``// Print the answer``   ``Console.WriteLine(cntTriplet[N]);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 7;``    ``primeTriplet(N);``}``}` `// This code is contributed by splevel62.`

## Javascript

 ``

Output:
`2`

Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up