Inelementary algebra, adifference of two squares is onesquared number (the number multiplied by itself)subtracted from another squared number. Every difference of squares may befactored as theproduct of thesum of the two numbers and thedifference of the two numbers:
Note that and can represent more complicated expressions, such that the difference of their squares can be factored as the product of their sum and difference. For example, given, and :
In the reverse direction, the product of any two numbers can be expressed as the difference between the square of theiraverage and the square of half their difference:
Theproof of the factorization identity is straightforward. Starting from theright-hand side, apply thedistributive law to getBy thecommutative law, the middle two terms cancel:leaving[1]The resulting identity is one of the most commonly used in mathematics. Among many uses, it gives a simple proof of theAM–GM inequality in two variables.
The proof holds not only for numbers, but for elements of anycommutative ring. Conversely, if this identity holds in aringR for all pairs of elementsa andb, thenR is commutative. To see this, apply the distributive law to the right-hand side of the equation and getFor this to be equal to, we must havefor all pairsa,b, soR is commutative.

The difference of two squares can also be illustrated geometrically as the difference of two square areas in aplane. In the diagram, the shaded part represents the difference between the areas of the two squares, i.e.. The area of the shaded part can be found by adding the areas of the two rectangles;, which can be factorized to. Therefore,.
Another geometric proof proceeds as follows. We start with the figure shown in the first diagram below, a large square with a smaller square removed from it. The side of the entire square is a, and the side of the small removed square is b; thus, the area of the shaded region is. A cut is made, splitting the region into two rectangular pieces, as shown in the second diagram. The larger piece, at the top, has width a and height a-b. The smaller piece, at the bottom, has width a-b and height b. Now the smaller piece can be detached, rotated, and placed to the right of the larger piece. In this new arrangement, shown in the last diagram below, the two pieces together form a rectangle, whose width is and whose height is. This rectangle's area is. Since this rectangle came from rearranging the original figure, it must have the same area as the original figure. Therefore,.[2]
The formula for the difference of two squares can be used for factoringpolynomials that contain the square of a first quantity minus the square of a second quantity. For example, the polynomial can be factored as follows:
As a second example, the first two terms of can be factored as, so we have:Moreover, this formula can also be used for simplifying expressions:
The difference of two squares is used to find thelinear factors of thesum of two squares, usingcomplex number coefficients.
For example, the complex roots of can be found using difference of two squares:
Therefore, the linear factors are and.
Since the two factors found by this method arecomplex conjugates, we can use this in reverse as a method of multiplying a complex number to get a real number. This is used to get real denominators in complex fractions.[3]
The difference of two squares can also be used, in reverse, in therationalising ofirrationaldenominators.[4] This is a method for removingsurds from expressions (or at least moving them), applying to division by some combinations involvingsquare roots.
For example, the denominator of can be rationalised as follows:
Here, the irrational denominator has been rationalised to.
The difference of two squares can also be used as an arithmetical shortcut. If two numbers have an easily squared average, their product can be rewritten as the difference of two squares. For example:
The difference of two consecutiveperfect squares is the sum of the twobasesn andn + 1. This can be seen as follows:
Therefore, the difference of two consecutive perfect squares is an odd number. Similarly, the difference of two arbitrary perfect squares is calculated as follows:
Therefore, the difference of two even perfect squares is a multiple of4 and the difference of two odd perfect squares is a multiple of8.

A ramification of the difference of consecutive squares,Galileo's law of odd numbers states that the distance covered by an object falling without resistance in uniform gravity in successive equal time intervals is linearly proportional to the odd numbers. That is, if a body falling from rest covers a certain distance during an arbitrary time interval, it will cover3,5,7, etc. times that distance in the subsequent time intervals of the same length.
From the equation for uniform linear acceleration, the distance coveredfor initial speed constant acceleration (acceleration due to gravity without air resistance), and time elapsed it follows that the distance is proportional to (in symbols,), thus the distance from the starting point are consecutive squares forinteger values of time elapsed.[5]
Several algorithms innumber theory and cryptography use differences of squares to find factors of integers and detect composite numbers. A simple example is theFermat factorization method, which considers the sequence of numbers, for. If one of the equals a perfect square, then is a (potentially non-trivial) factorization of.
This trick can be generalized as follows. If mod and mod, then is composite with non-trivial factors and. This forms the basis of several factorization algorithms (such as thequadratic sieve) and can be combined with theFermat primality test to give the strongerMiller–Rabin primality test.

The identity also holds ininner product spaces over thefield ofreal numbers, such as fordot product ofEuclidean vectors:
The proof is identical. For the special case thata andb have equalnorms (which means that their dot squares are equal), this demonstratesanalytically the fact that two diagonals of arhombus areperpendicular. This follows from the left side of the equation being equal to zero, requiring the right side to equal zero as well, and so the vector sum ofa +b (the long diagonal of the rhombus) dotted with the vector differencea −b (the short diagonal of the rhombus) must equal zero, which indicates the diagonals are perpendicular.

Ifa andb are two elements of a commutative ring, then[6]
The second factor looks similar to thebinomial expansion of, except that it does not include thebinomial coefficients.