Why is every prime number ($5$ and higher) divisible by $24$ (into a whole integer) when you square it and subtract $1$? [duplicate]

I asked this question on puzzling.se and someone suggested I post it here:

I discovered this by accident, when trying to create a formula that generates prime numbers (an impossible task, I know).

But, I find it very interesting that you take any prime number 5 and greater, then you square it and subtract 1, dividing it by 24 always results in a whole integer.

For example:

5 x 5 = 25 - 1 = 24 / 24 = 1
7 x 7 = 49 - 1 = 48 / 24 = 2
11 x 11 = 121 - 1 = 120 / 24 = 50

The result is always a whole number, regardless of how high the prime number is.

Can someone explain why this is so, mathematically? This does not seem possible (to me). And if this is really true, why can I find nothing written about it?

I have never heard of this theorem before, and nothing is mentioned on Wikipedia or other sources. But perhaps this could be a helpful in reducing $33\%$ of the possibilities when trying to find or prove large prime numbers, computationally.

UPDATE: lab made the observation that all resulting numbers are apparently also divisible by $24$ .. interesting

UPDATE: following lab's observation , I tried $24 \times 24 = 576 + 1 = 577$ ( a prime number) .. interesting and.. $576 \times 576 + 1 = 331,777$ which is also a prime

Further extrapolating in the gap of numbers between $2$ and $50$:

primes:

24 x 3 = 72 + 1 = 73  72 - 1 = 71  72 + 7 = 79
24 x 4 = 96 + 1 = 97   96 + 5 = 101  96 + 7 = 103   
24 x 5 = 120 - 7 = 113  120 + 7 = 121
24 x 6 = 144 - 7 = 137  144 + 7 = 151
24 x 7 = 168 - 1 = 167  168 + 5 = 173
24 x 8 = 192 - 1 = 191  192 + 5 = 197
24 x 9 = 216 + 7 = 223

Not a perfect formula for finding primes, but seems to be effective :-)


Solution 1:

Your observation holds for any integer that is not divisible by $3$, not just primes. Indeed, $n^2-1=(n+1)(n-1)$ and one of the numbers $n+1$, $n-1$ is a multiple of $3$ unless $n$ is a multiple of $3$.

Solution 2:

Any integer can be expressed as $6n,6n\pm1,6n\pm2,6n+3$ where $n$ is any integer

Observe that, any prime number $>3,$ can be expressed as $6n\pm1$

Now, $(6n\pm1)^2=36n^2\pm12n+1=24n^2+24\dfrac{n(n\pm1)}2+1\equiv1\pmod{24}$

So, any integer of the form $6n\pm1$ will satisfy the proposition, we don't even need primes