Given a positive integer n, count distinct number of pairs (x, y) that satisfy following conditions :
- (x + y) is a prime number.
- (x + y) < n
- x != y
- 1 <= x, y
Input : n = 6 Output : 3 prime pairs whose sum is less than 6 are: (1,2), (1,4), (2,3) Input : 12 Output : 11 prime pairs whose sum is less than 12 are: (1,2), (1,4), (2,3), (1,6), (2,5), (3,4), (1,10), (2,9), (3,8), (4,7), (5,6)
1) Find all prime numbers less than n using Sieve of Sundaram 2) For each prime number p, count distinct pairs that sum up to p. For any odd number n, number of distinct pairs that add upto n are n/2 Since, a prime number is a odd number, the same applies for it too.
For prime number p = 7
distinct pairs that add upto p: p/2 = 7/2 = 3
The three pairs are (1,6), (2,5), (3,4)
For prime number p = 23
distinct pairs that add upto p: p/2 = 23/2 = 11
Number of prime pairs: 11
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Queries to count the number of unordered co-prime pairs from 1 to N
- Count of pairs in an Array whose sum is Prime
- Count Sexy Prime Pairs in the given array
- Count twin prime pairs in an Array
- Count pairs in an array such that at least one element is prime
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Number of co-prime pairs in an array
- Number of prime pairs in an array
- Number of Co-prime pairs from 1 to N with product equals to N
- Generate K co-prime pairs of factors of a given number
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Count of prime digits of a Number which divides the number
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Given two arrays count all pairs whose sum is an odd number
- Count pairs with Bitwise AND as ODD number
- Count of the non-prime divisors of a given number
- Count of Prime digits in a Number
- Count all pairs of divisors of a number N whose sum is coprime with N