Why is every prime number ($5$ and higher) divisible by $24$ (into a whole integer) when you square it and subtract $1$? 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 :-)
 A: I'll use Fermat's little theorem:
"If $a \in \Bbb N$ is any natural number, and $p$ is a prime number, such that $gcd(a,p) = 1$, then $a^{p - 1} \equiv 1 \ (mod \ p)$".
We have that $3$ is prime, and every prime $p > 3$ is coprime with $3$. Consequently:
$$\forall \ prime \ p > 3, \ p^{3 - 1} \equiv 1 \ (mod \ 3), \ i.e. \ p^2 \equiv 1 \ (mod \ 3)$$
This means that for any prime number $p > 3$, $p^2$ leaves remainder $1$ upon division by $3$, i.e. $p^2 - 1$ leaves remainder $0$ upon division by $3$.
A: This answer walks through some simple math, and pulls the answer out in the last paragraph:
Squaring n and subtracting one gives n²-1, which can be rewritten as (n+1)(n-1).
If n doesn't divide by 3, then one of n+1 and n-1 divides by 3 (because of the three consecutive numbers n-1, n and n+1, exactly one of them must divide by 3).
If n doesn't divide by 2, then both n+1 and n-1 divide by 2, and (exactly) one of those divides 4.
This means that if n doesn't divide 2 or 3 (for example, if it's a prime number that's 5 or higher), then (n+1)(n-1) divides by 2, 3, and 4 (or to put it more clearly, it divides by 3 and 8), and hence divides 24.
A: 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$.
A: 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
