Given two positive consecutive numbers M and N, the task is to find the square difference of the two numbers without computing the square of those numbers.

**Examples:**

Input:N = 4, M = 5

Output:9

Explanation:

5^{2}– 4^{2}= 25 – 16 = 9.

Input:N = 999999999, M = 1000000000

Output:1999999999

**Approach:** The idea is to use the algebraic expression to solve this problem. It is given in the question that M = N + 1. Therefore:

- The value to be computed is M
^{2}– N^{2}. - On replacing M with N + 1, the above equation becomes:
(N + 1)

^{2}- N^{2} - (N + 1)
^{2}can be expanded to:N

^{2}+ 1^{2}+ 2 * N - N^{2} - On simplifying, the above equation becomes:
1 + 2 * N 1 + N + N (1 + N) + N M + N

- Therefore, we simply need to compute and return the value of M + N.

Below is the implementation of the above approach:

## CPP

`// C++ program to find the square ` `// difference of two large ` `// consecutive numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `typedef` `long` `long` `l; ` ` ` `// Function to find the square ` `// difference of two large ` `// consecutive numbers ` `l difference(l M, l N) ` `{ ` ` ` `return` `M + N; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `l M = 999999999; ` ` ` `l N = 1000000000; ` ` ` ` ` `cout << difference(M, N) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the square ` `// difference of two large ` `// consecutive numbers ` ` ` ` ` `class` `GFG{ ` ` ` `// Function to find the square ` `// difference of two large ` `// consecutive numbers ` `static` `long` `difference(` `long` `M, ` `long` `N) ` `{ ` ` ` `return` `M + N; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `M = ` `999999999` `; ` ` ` `long` `N = ` `1000000000` `; ` ` ` ` ` `System.out.print(difference(M, N) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the square ` `# difference of two large ` `# consecutive numbers ` ` ` `# Function to find the square ` `# difference of two large ` `# consecutive numbers ` `def` `difference(M, N): ` ` ` `return` `M ` `+` `N ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `M ` `=` `999999999` ` ` `N ` `=` `1000000000` ` ` ` ` `print` `(difference(M, N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the square ` `// difference of two large ` `// consecutive numbers ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to find the square ` `// difference of two large ` `// consecutive numbers ` `static` `long` `difference(` `long` `M, ` `long` `N) ` `{ ` ` ` `return` `M + N; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `long` `M = 999999999; ` ` ` `long` `N = 1000000000; ` ` ` ` ` `Console.Write(difference(M, N) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1999999999

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.

## Recommended Posts:

- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Count the number of ordered sets not containing consecutive numbers
- Sum of minimum difference between consecutive elements of an array
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count subarrays with sum as difference of squares of two numbers
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Knapsack with large Weights
- Find N % 4 (Remainder with 4) for a large value of N
- Comparing X^Y and Y^X for very large values of X and Y
- Uniform-Cost Search (Dijkstra for large Graphs)
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Check if a binary string has two consecutive occurrences of one everywhere
- Largest lexicographical string with at most K consecutive elements
- Maximize the maximum among minimum of K consecutive sub-arrays

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.