给定两个整数L和R ,任务是找到在[L,R]范围内的质数个数,该质数可以由两个数的两个平方之和表示。
例子:
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
方法:
可以使用费马小定理解决该给定的问题,该定理指出,如果p满足以下方程式,则质数p可以表示为两个平方的和:
(p – 1) % 4 == 0
请按照以下步骤解决问题:
- 遍历范围[L,R] 。
- 对于每个数字,请检查它是否不是质数。
- 如果发现是这样,请检查素数的格式是否为4K +1 。如果是sp,请增加count 。
- 遍历整个范围后,请打印计数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
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
输出:
6
时间复杂度: O(N 3/2 )
辅助空间: O(1)