So whenever I square any number and subtract previous number of the original I always end up with a prime number. Why is it so? Let's say a number $x$ is squared and $x - 1$ is subtracted from it the result would be prime.
3 Answers
Maybe you've gotten lucky and only tested numbers drawn from Sloane's A055494:
A055494 Numbers $n$ such that $n^2 - n + 1$ is prime. ... 2, 3, 4, 6, 7, 9, 13, 15, 16, 18, 21, 22, 25, 28, 34, 39, 42, 51, 55, 58, 60, 63, 67, 70, 72, 76, 78, 79, 81, 90, 91, 100, 102, 106, 111, 112, 118, 120, 132, 139, 142, 144, 148, 151, 154, 156, 162, 163, 165, 168, 169, 174, 177, 189, 190, 193, 195, 204, 207, 210, 216
Obviously $n^2 - n$ is a multiple of $n$. But $n^2 - n + 1$ is coprime to $n$, meaning that it has no prime factors in common with $n$. So it will sometimes happen that $n^2 - n + 1$ is actually itself prime.
For example, $8^2 - 8 + 1 = 57 = 3 \times 19$. So 8 is a power of 2, while 57 is an odd number, the product of two odd primes. 57 is not prime, but it still has no prime factors in common with 8.
Compare that to 9, which gives us $9^2 - 9 + 1 = 73$, which is indeed prime.
Note that
$$x^2-(x-1)=(x+\sqrt{x-1})(x-\sqrt{x-1})$$
thus for counterexamples it suffices that $x-1$ is a square.
Select[Table[n^2 - (n - 1), {n, 50}], Not[PrimeQ[#]] &]
for plenty more counterexamples. $\endgroup$