Skip to content
Related Articles

Related Articles

Check if difference of areas of two squares is prime
  • Difficulty Level : Easy
  • Last Updated : 13 Nov, 2018

Given two squares with side lengths a and b (a > b). The task is to check if difference of their areas is prime or not. Here side length could be large ( 1 < b < a < 1012).

Examples:

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

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

Approach: Since the sides are a and b. Therefore, difference of their areas = (a2 – b2), which can be expressed as (a – b)(a + b) . This is prime if and only if a – b = 1 and a + b is a prime . Since a+b is at most 2×1012, we can use trial division to check its primality.

Below is the implementation of the above idea:

C++




// C++ program to check if difference of
// areas of two sqaures is prime or not
// when side length is large
  
#include <bits/stdc++.h>
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




<?php
// PHP program to check if difference 
// of areas of two sqaures is prime 
// or not when side length is large
function 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 || $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 check if difference
// of areas of square is prime
function isDiffPrime($a, $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
$a = 6;
$b = 5;
  
if (isDiffPrime($a, $b))
    echo "Yes";
else
    echo "No";
  
// This code is contributed by ihritik
?>


Output:

Yes

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :