Count of primes in a given range that can be expressed as sum of perfect squares
Last Updated :
16 Apr, 2021
Given two integers L and R, the task is to find the number of prime numbers in the range [L, R] that can be represented by the sum of two squares of two numbers.
Examples:
Input: L = 1, R = 5
Output: 1
Explanation:
Only prime number that can be expressed as sum of two perfect squares in the given range is 5 (22 + 12)
Input: L = 7, R = 42
Output: 5
Explanation:
The prime numbers in the given range that can be expressed as sum of two perfect squares are:
13 = 22 + 32
17 = 12 + 42
29 = 52 + 22
37 = 12 + 62
41 = 52 + 42
Approach:
The given problem can be solved using Fermat’s Little theorem, which states that a prime number p can be expressed as the sum of two squares if p satisfies the following equation:
(p – 1) % 4 == 0
Follow the steps below to solve the problem:
- Traverse the range [L, R].
- For every number, check if it is a prime number of not.
- If found to be so, check if the prime number is of the form 4K + 1. If sp, increase count.
- After traversing the complete range, print count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool sumSquare( int p)
{
return (p - 1) % 4 == 0;
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
int countOfPrimes( int L, int R)
{
int count = 0;
for ( int i = L; i <= R; i++) {
if (isPrime(i)) {
if (sumSquare(i))
count++;
}
}
return count;
}
int main()
{
int L = 5, R = 41;
cout << countOfPrimes(L, R);
}
|
Java
import java.util.*;
class GFG{
static boolean sumSquare( int p)
{
return (p - 1 ) % 4 == 0 ;
}
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static int countOfPrimes( int L, int R)
{
int count = 0 ;
for ( int i = L; i <= R; i++)
{
if (isPrime(i))
{
if (sumSquare(i))
count++;
}
}
return count;
}
public static void main(String[] args)
{
int L = 5 , R = 41 ;
System.out.println(countOfPrimes(L, R));
}
}
|
Python3
def sumsquare(p):
return (p - 1 ) % 4 = = 0
def isprime(n):
if n < = 1 :
return False
if n < = 3 :
return True
if (n % 2 = = 0 ) or (n % 3 = = 0 ):
return False
i = 5
while (i * i < = n):
if ((n % i = = 0 ) or
(n % (i + 2 ) = = 0 )):
return False
i + = 6
return True
def countOfPrimes(L, R):
count = 0
for i in range (L, R + 1 ):
if (isprime(i)):
if sumsquare(i):
count + = 1
return count
if __name__ = = '__main__' :
L = 5
R = 41
print (countOfPrimes(L, R))
|
C#
using System;
class GFG{
static bool sumSquare( int p)
{
return (p - 1) % 4 == 0;
}
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static int countOfPrimes( int L, int R)
{
int count = 0;
for ( int i = L; i <= R; i++)
{
if (isPrime(i))
{
if (sumSquare(i))
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int L = 5, R = 41;
Console.WriteLine(countOfPrimes(L, R));
}
}
|
Javascript
<script>
function sumSquare(p) {
return (p - 1) % 4 == 0;
}
function isPrime(n) {
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function countOfPrimes(L , R) {
var count = 0;
for ( var i = L; i <= R; i++) {
if (isPrime(i)) {
if (sumSquare(i))
count++;
}
}
return count;
}
var L = 5, R = 41;
document.write(countOfPrimes(L, R));
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...