Count of primes in a given range that can be expressed as sum of perfect squares
Last Updated :
15 Jul, 2025
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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a prime number
// satisfies the condition to be
// expressed as sum of two perfect squares
bool sumSquare(int p)
{
return (p - 1) % 4 == 0;
}
// Function to check if a
// number is prime or not
bool isPrime(int n)
{
// Corner cases
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;
}
// Function to return the count of primes
// in the range which can be expressed as
// the sum of two squares
int countOfPrimes(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++) {
// If i is a prime
if (isPrime(i)) {
// If i can be expressed
// as the sum of two squares
if (sumSquare(i))
count++;
}
}
// Return the count
return count;
}
// Driver Code
int main()
{
int L = 5, R = 41;
cout << countOfPrimes(L, R);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check if a prime number
// satisfies the condition to be
// expressed as sum of two perfect
// squares
static boolean sumSquare(int p)
{
return (p - 1) % 4 == 0;
}
// Function to check if a
// number is prime or not
static boolean isPrime(int n)
{
// Corner cases
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;
}
// Function to return the count of primes
// in the range which can be expressed as
// the sum of two squares
static int countOfPrimes(int L, int R)
{
int count = 0;
for(int i = L; i <= R; i++)
{
// If i is a prime
if (isPrime(i))
{
// If i can be expressed
// as the sum of two squares
if (sumSquare(i))
count++;
}
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
int L = 5, R = 41;
System.out.println(countOfPrimes(L, R));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the
# above approach
# Function to check if a prime number
# satisfies the condition to be
# expressed as sum of two perfect
# squares
def sumsquare(p):
return (p - 1) % 4 == 0
# Function to check if a
# number is prime or not
def isprime(n):
# Corner cases
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
# Function to return the count of primes
# in the range which can be expressed as
# the sum of two squares
def countOfPrimes(L, R):
count = 0
for i in range(L, R + 1):
# If i is a prime
if (isprime(i)):
# If i can be expressed
# as the sum of two squares
if sumsquare(i):
count += 1
# Return the count
return count
# Driver code
if __name__=='__main__':
L = 5
R = 41
print(countOfPrimes(L, R))
# This code is contributed by virusbuddah_
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if a prime number
// satisfies the condition to be
// expressed as sum of two perfect
// squares
static bool sumSquare(int p)
{
return (p - 1) % 4 == 0;
}
// Function to check if a
// number is prime or not
static bool isPrime(int n)
{
// Corner cases
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;
}
// Function to return the count of primes
// in the range which can be expressed as
// the sum of two squares
static int countOfPrimes(int L, int R)
{
int count = 0;
for(int i = L; i <= R; i++)
{
// If i is a prime
if (isPrime(i))
{
// If i can be expressed
// as the sum of two squares
if (sumSquare(i))
count++;
}
}
// Return the count
return count;
}
// Driver code
public static void Main(String[] args)
{
int L = 5, R = 41;
Console.WriteLine(countOfPrimes(L, R));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript program to implement
// the above approach
// Function to check if a prime number
// satisfies the condition to be
// expressed as sum of two perfect
// squares
function sumSquare(p) {
return (p - 1) % 4 == 0;
}
// Function to check if a
// number is prime or not
function isPrime(n) {
// Corner cases
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 to return the count of primes
// in the range which can be expressed as
// the sum of two squares
function countOfPrimes(L , R) {
var count = 0;
for (var i = L; i <= R; i++) {
// If i is a prime
if (isPrime(i)) {
// If i can be expressed
// as the sum of two squares
if (sumSquare(i))
count++;
}
}
// Return the count
return count;
}
// Driver code
var L = 5, R = 41;
document.write(countOfPrimes(L, R));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Similar Reads
Find sum of all odd perfect squares in the range [L, R] Given two integers L and R. The task is to find the sum of all the odd numbers which are perfect square in the range [L, R]. Examples: Input: L = 1, R = 9Output: 10Explanation: The odd Numbers in the range are 1, 3, 5, 7, 9 and only 1, 9 are perfect squares of 1, 3. So, 1 + 9 = 10. Input: L = 50, R
8 min read
Sum of all Perfect Squares lying in the range [L, R] for Q queries Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which signifies the range [L, R], the task is to find the sum of all perfect squares lying in this range. Examples: Input: Q = 2, arr[][] = {{4, 9}, {4, 16}} Output: 13 29 Explanation: From 4 to 9: only 4
7 min read
Sum of all Perfect Squares lying in the range [L, R] for Q queries Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which signifies the range [L, R], the task is to find the sum of all perfect squares lying in this range. Examples: Input: Q = 2, arr[][] = {{4, 9}, {4, 16}} Output: 13 29 Explanation: From 4 to 9: only 4
7 min read
Sum of all Primes in a given range using Sieve of Eratosthenes Given a range [l, r], the task is to find the sum of all the prime numbers in the given range from l to r both inclusive.Examples: Input : l = 10, r = 20Output : 60Explanation: Prime numbers between [10, 20] are: 11, 13, 17, 19Therefore, sum = 11 + 13 + 17 + 19 = 60Input : l = 15, r = 25Output : 59E
1 min read
Sum of all Primes in a given range using Sieve of Eratosthenes Given a range [l, r], the task is to find the sum of all the prime numbers in the given range from l to r both inclusive.Examples: Input : l = 10, r = 20Output : 60Explanation: Prime numbers between [10, 20] are: 11, 13, 17, 19Therefore, sum = 11 + 13 + 17 + 19 = 60Input : l = 15, r = 25Output : 59E
1 min read
Count primes that can be expressed as sum of two consecutive primes and 1 Given a number N. The task is to count the number of prime numbers from 2 to N that can be expressed as a sum of two consecutive primes and 1.Examples: Input: N = 27 Output: 2 13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.Input: N = 34 Output: 3 13 = 5 + 7 + 1, 19 = 7 + 11 + 1 an
9 min read