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 = 5Output:1Explanation:

Only prime number that can be expressed as sum of two perfect squares in the given range is 5 (2^{2}+ 1^{2})Input:L = 7, R = 42Output:5Explanation:

The prime numbers in the given range that can be expressed as sum of two perfect squares are:

13 = 2^{2}+ 3^{2}

17 = 1^{2}+ 4^{2}

29 = 5^{2}+ 2^{2}

37 = 1^{2}+ 6^{2}

41 = 5^{2}+ 4^{2}

**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); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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_` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** O(N^{3/2}) **Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Check if an integer can be expressed as a sum of two semi-primes
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Check if a number can be expressed as sum of two Perfect powers
- Count elements in an Array that can be represented as difference of two perfect squares
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Print all perfect squares from the given range
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Numbers within a range that can be expressed as power of two numbers
- Count of perfect squares of given length
- Length of largest sub-array having primes strictly greater than non-primes
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Count numbers which can be represented as sum of same parity primes
- Check if a number can be expressed as a sum of consecutive numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers
- Count number less than N which are product of perfect squares

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.