📜  检查两个正方形的面积之差是否为素数

📅  最后修改于: 2021-05-04 20:04:07             🧑  作者: Mango

给定两个具有边长的正方形ab (a> b)。任务是检查它们的面积差异是否是主要的。在这里边长可能会很大(1 12 )。

例子

Input : a = 6, b = 5
Output : Yes

Input : a = 61690850361, b = 24777622630    
Output : No

方法:由于双方ab 。因此,它们的面积之差=(a 2 – b 2 ),可以表示为(a – b)(a + b)。当且仅当a – b = 1并且a + b是素数时,才是素数。由于a + b最多为2×10 12 ,因此我们可以使用试验除法来检查其素数。

下面是上述想法的实现:

C++
// C++ program to check if difference of
// areas of two sqaures is prime or not
// when side length is large
  
#include 
using namespace std;
  
// Function to check if number is prime
bool isPrime(long long int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (long long int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// Function to check if difference of areas of
// square is prime
bool isDiffPrime(long long int a, long long int b)
{
    // when a+b is prime and a-b is 1
    if (isPrime(a + b) && a - b == 1)
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    long long int a = 6, b = 5;
  
    if (isDiffPrime(a, b))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check if difference
// of areas of two sqaures is prime or 
// not when side length is large
class GFG
{
      
// Function to check if number 
// is prime
static boolean isPrime(long n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (long i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// Function to check if difference 
// of areas of square is prime
static boolean isDiffPrime(long a, long b)
{
    // when a+b is prime and a-b is 1
    if (isPrime(a + b) && a - b == 1)
        return true;
    else
        return false;
}
  
// Driver code
public static void main(String []args)
{
    long a = 6, b = 5;
  
    if (isDiffPrime(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by ihritik


C#
// C# program to check if difference
// of areas of two sqaures is prime 
// or not when side length is large
using System;
  
class GFG
{
// Function to check if number
// is prime
static bool isPrime(long n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we 
    // can skip middle five numbers 
    // in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (long i = 5; 
              i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// Function to check if difference 
// of areas of square is prime
static bool isDiffPrime(long a, long b)
{
    // when a+b is prime and a-b is 1
    if (isPrime(a + b) && a - b == 1)
        return true;
    else
        return false;
}
  
// Driver code
public static void Main()
{
    long a = 6, b = 5;
  
    if (isDiffPrime(a, b))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by ihritik


Python3
# Python3 program to check if 
# difference of areas of two 
# sqaures is prime or not when
# side length is large
  
def isPrime(n) : 
      
    # Corner cases 
    if (n <= 1) : 
        return False
    if (n <= 3) : 
        return True
  
    # This is checked so that we  
    # can skip middle five numbers
    # in below loop 
    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 = i + 6
  
    return True
  
# Function to check if difference 
# of areas of square is prime
def isDiffPrime(a, b):
  
    # when a+b is prime and a-b is 1
    if (isPrime(a + b) and a - b == 1):
        return True
    else:
        return False
  
# Driver code
a = 6
b = 5
  
if (isDiffPrime(a, b)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by ihritik


PHP


输出:
Yes