Check n^2 – m^2 is prime or not

Given two integers n and m. Check n^2 – m^2 is prime or not. n and m can be very large.

Examples:

```Input : n = 6, m = 5
Output : YES

Input : n = 16, m = 13
Output : NO
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to fist compute n^2 – m^2, then check if it is prime or not. n^2 – m^2 might be very large – it might not even fit into 64-bit integer. Checking primality for it certainly cannot be performed naively.

A better solution is to express n^2 – m^2 as (n-m)(n+m). This is prime if and only if n-m = 1 and n+m is a prime.

C++

 `// CPP program to find n^2 - m^2 ` `// is prime or not. ` `#include ` `using` `namespace` `std; ` ` `  `// Check a number is prime or not ` `bool` `isprime(``int` `x) ` `{ ` `    ``// run a loop upto square of given number ` `    ``for` `(``int` `i = 2; i * i <= x; i++) ` `        ``if` `(x % i == 0) ` `            ``return` `false``; ` `    ``return` `true``; ` `} ` ` `  `// Check if n^2 - m^2 is prime ` `bool` `isNSqMinusnMSqPrime(``int` `m, ``int` `n) ` `{ ` `    ``if` `(n - m == 1 and isprime(m + n)) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `m = 13, n = 16; ` `    ``if` `(isNSqMinusnMSqPrime(m, n)) ` `        ``cout << ``"YES"``; ` `    ``else` `        ``cout << ``"NO"``; ` ` `  `    ``return` `0; ` `} `

Java

 `// Java program to find n^2 - m^2 ` `// is prime or not. ` ` `  `class` `GFG ` `{ ` `        ``// Check if a number is prime or not ` `        ``static` `boolean` `isprime(``int` `x) ` `        ``{ ` `            ``// run a loop upto square of given number ` `            ``for` `(``int` `i = ``2``; i * i <= x; i++) ` `                ``if` `(x % i == ``0``) ` `                    ``return` `false``; ` `            ``return` `true``; ` `        ``} ` `         `  `        ``// Check if n^2 - m^2 is prime ` `        ``static` `boolean` `isNSqMinusnMSqPrime(``int` `m, ``int` `n) ` `        ``{ ` `            ``if` `(n - m == ``1` `&& isprime(m + n)) ` `                ``return` `true``; ` `            ``else` `                ``return` `false``; ` `        ``} ` `         `  `        ``// Driver code ` `        ``public` `static` `void`  `main(String [] args) ` `        ``{ ` `            ``int` `m = ``13``, n = ``16``; ` `            ``if` `(isNSqMinusnMSqPrime(m, n)) ` `                ``System.out.println(``"YES"``); ` `            ``else` `                ``System.out.println(``"NO"``); ` `         `  `        ``} ` `} ` ` `  `// This code is contributed ` `// by ihritik `

Python3

 `# Python program to find n^2 - m^2  ` `# is prime or not.  ` ` `  `# Check a number is prime or not  ` `def` `isprime(x):  ` ` `  `    ``# run a loop upto square  ` `    ``# of given number  ` `    ``for` `i ``in` `range``(``2``, math.sqrt(x)):  ` `        ``if` `(x ``%` `i ``=``=` `0``) : ` `            ``return` `False``;  ` `    ``return` `True``;  ` ` `  `# Check if n^2 - m^2 is prime  ` `def` `isNSqMinusnMSqPrime( m, n):  ` ` `  `    ``if` `(n ``-` `m ``=``=` `1` `and` `isprime(m ``+` `n)):  ` `        ``return` `True``;  ` `    ``else``: ` `        ``return` `False``;  ` ` `  `# Driver code  ` `m ``=` `13``; ` `n ``=` `16``;  ` `if` `(isNSqMinusnMSqPrime(m, n)) : ` `    ``print` `( ``"YES"``);  ` `else``: ` `    ``print` `(``"NO"``);  ` ` `  `# This code is contributed  ` `# by Shivi_Aggarwal `

C#

 `// C# program to find n^2 - m^2  ` `// is prime or not.  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `// Check if a number is prime or not  ` `static` `bool` `isprime(``int` `x)  ` `{  ` `    ``// run a loop upto square ` `    ``// of given number  ` `    ``for` `(``int` `i = 2; i * i <= x; i++)  ` `        ``if` `(x % i == 0)  ` `            ``return` `false``;  ` `    ``return` `true``;  ` `}  ` ` `  `// Check if n^2 - m^2 is prime  ` `static` `bool` `isNSqMinusnMSqPrime(``int` `m,  ` `                                ``int` `n)  ` `{  ` `    ``if` `(n - m == 1 && isprime(m + n))  ` `        ``return` `true``;  ` `    ``else` `        ``return` `false``;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `m = 13, n = 16;  ` `    ``if` `(isNSqMinusnMSqPrime(m, n))  ` `        ``Console.Write(``"YES"``);  ` `    ``else` `        ``Console.Write(``"NO"``);  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by Smitha `

PHP

 ` `

Output:

```NO
```

Time Complexity: O(sqrt(n+m))

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.