Check if difference of areas of two squares is prime
Given two squares with side lengths
and
(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
and
. 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 = 6b = 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 ?> |
Yes
Recommended Posts:
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check whether a number can be represented by sum of two squares
- Check if the sum of perfect squares in an array is divisible by x
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Sum of Areas of Rectangles possible for an array
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Co-prime pair with given sum minimum difference
- Queries for maximum difference between prime numbers in given ranges
- First N natural can be divided into two sets with given difference and co-prime sums
- Pair of prime numbers with a given sum and minimum absolute difference
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.
Improved By : ihritik



