Prime
A number $$p \in \mathbb{N}_{2}$$ is said to be prime if it's only divisors are $$1$$ and $$p$$ symbolically that is $\forall d \in \mathbb{ N } _ 1, \left( d \mid p \right) \implies d \in \left\{ 1, p \right\}$
Set of Primes
We denote the set of primes as $$\mathbb{ P }$$
If a Prime Divides Another they are Equal
Suppose that $$p, q \in \mathbb{ P }$$, then $p \mid q \implies p = q$
Since $$p \mid q$$ then we know that $$p = 1$$ or $$p = q$$ the former is impossible since $$\mathbb{ P } \subseteq \mathbb{ N } _ 2$$ therefore $$p = q$$ as needed.
Primes of the Form 4k Plus Something
Suppose that $$p \in \mathbb{ P } _ 3$$ then there exists some $$k \in \mathbb{ N } _ 0$$ such that $p = 4k + 1 \text{ or } p = 4k + 3$
By the quotient remainder theorem we have that $p = 4 q + r$ where $$q \in \mathbb{ Z }$$ and $$r \in \left[ 0, \ldots , q - 1 \right]$$, when $$p = 3$$ the only possibility is that $$q = 0$$ and $$r = 3$$, on the other hand for any $$p \ge 5$$ we have that $$q \ge 1$$, now if $$q = 0$$ or $$r = 2$$ then the prime is divisible by $$2$$ and which makes $$p$$ not prime, which is a contradiction, therefore $$r \in \left\{ 1, 3 \right\}$$.
Increasing Prime Function
We let the function $$p _ i: \mathbb{ N } _ 1 \to \mathbb{ P }$$ be an increasing function such that $$p _ 1 = 2$$ and there are no primes between $$p _ i$$ and $$p _ { i + 1 }$$.

This function looks like $$2, 3, 5, 7, 11, 13, 17, \ldots$$

Composite
We say that $$n \in \mathbb{ Z }$$ where $$\left\lvert n \right\rvert \ge 2$$ is composite if there exists an $$x \in \left\{ 2, \ldots , n - 1 \right\}$$ such that $$x \mid n$$

The composites union the primes is $$\mathbb{ Z } \setminus \left\{ -1, 0, 1 \right\}$$

Composite iff Not Prime
Let $$a \in \mathbb{ N } _ 1$$ then $$a$$ is composite iff $$a$$ is not prime
We know that it's always true that $$a \mid a$$ and $$1 \mid a$$ therefore if $$a$$ is not prime iff it has some other divisor other than $$1$$ or $$a$$ iff $$a$$ is composite
A Prime is Relatively Prime to Anything Else That is Not a Multiple of It
Suppose that $$p$$ is prime and $$n \in \mathbb{ Z }$$ then $$p \nmid n$$ iff $$\gcd \left( p, n \right) = 1$$
A Prime Divisor of a Product Divides at Least One
If $$p, a, b \in \mathbb{ Z }$$ and we know $$p$$ is prime and $$p \mid ab$$ then $$p \mid a$$ or $$p \mid b$$.
If $$p$$ were to divide $$a$$, the proof is over so we assume that it does not. therefore $$\gcd \left( p, a \right) = 1$$ and then $$1 = as + pt$$, then $$b = \left( ab \right) s + ptb = \left( k \cdot p \right) s + ptb$$, so then $$p$$ divides the right hand side, therefore $$p \mid b$$, as needed.
Every Number is Divisible by a Prime
For any $$n \in \mathbb{ N } _ 2$$ there is a prime $$p$$ such that $$p \mid n$$

To show existance we will use the well ordering principle so consider the set $S := \left\{ c \in \mathbb{ N } _ 2 : c \mid n \right\}$ observe that $$n \in S$$ so therefore $$S \neq \emptyset$$ and therefore by the well ordering principle it has a least element $$p \in S$$ therefore by the definition of $$S$$ we know that $$p \mid n$$, lets show that $$p$$ is prime.

Let $$d \in \mathbb{ N } _ 1, d \mid p$$ if $$d = 1$$, then $$d \in \left\{ 1, p \right\}$$ so all is well, on the other hand if $$d \gt 1$$ so that $$d \in \mathbb{ N } _ 2$$ then since we already knew that $$p \mid n$$ and that division is transitive then we know that $$d \mid n$$ which means that $$d \in S$$, since $$p$$ is the minimal element of $$S$$ then $$p \le d$$ at the same time since $$d \mid p$$ then we know that $$d \le p$$, thus since $$\le$$ is anti-symmetric, then $$d = p$$.

Thus $$p$$ is prime, and $$p \mid n$$ as needed.

Every Number is a Product of Primes
For every $$n \in \mathbb{ N } _ 2$$ $n := p _ 1 \cdot p _ 2 \cdot \cdots \cdot p _ k$ for some $$k \in \mathbb{ N } _ 1$$

We prove the statement true via strong induction, for the base case of $$n = 2$$ we see that $$p _ 1 = 2$$ works, namely that $$2 = 2$$.

Now suppose for some $$j \in \mathbb{ N } _ 2$$ the statement we'd like to prove holds for $$\left\{ 2, \ldots , j \right\}$$ we'll prove that the statement holds true for $$j + 1$$, so consider $$j + 1$$, if it is prime, then we are done, as we can take it as the singleton product, elsewise it is not prime, and therefore it is composite so we get $$a, b \in \mathbb{ N } _ 2$$ such that $$a b = j + 1$$ by our induction hypothesis we know that the statement holds true for $$a, b$$ and thus we get $$a = p _ 1 \cdot \cdots \cdot p _ n$$ and $$b = q _ 1 \cdot \cdots \cdot q _ m$$ for some $$m, n \in \mathbb{ N } _ 1$$, thus $$j + 1 = p _ 1 \cdots p _ n q _ 1 \cdots q _ m$$ so that $$j + 1$$ is the product of $$m + n$$ primes as needed.

There are Infinitely Many Primes
As per title.
Suppose that there were finitely many primes, and we listed all of them here: $$p _ 1, \ldots p _ k$$ for $$k \in \mathbb{ N } _ 1$$ and consider $M := p _ 1 \cdots p _ k + 1$ For every $$i \in \left[ 1, k \right]$$ we have that $$M ~\%~ p _ i = 1$$ therefore $$p _ i \nmid P$$, at the same time $$M$$ must be divisible by some prime $$p ^ \prime$$ but clearly $$p ^ \prime \neq p _ i$$ for any $$i \in \left[ 1, k \right]$$ or else $$M ~\%~ p _ i = 0$$ which would be a contradiction, since it's not equal to any of the primes we listed, this is a contradiction because we know that it contained all primes, therefore our assumption is incorrect and there must be infinitely many primes.
There is a Prime Between n and n Factorial
For any $$n \in \mathbb{ N } _ 3$$ there is a prime number $$p$$ such that $$n \lt p \lt n!$$
Let $$N = \left\{ p \in \mathbb{ P }: p \le n \right\}$$ since $$n \ge 3$$ then at least $$2, 3 \in N$$ so it is non-empty. Since $$n! \ge 6$$ then we know that $$n ! - 1 \in \mathbb{ N } _ 2$$ and therefore there is a prime number which divides it, say it is $$q$$, for any $$p \in N$$ we know that $$n ! - 1 \equiv -1 \pmod{ p }$$ therefore $$q \notin N$$ thus $$q \gt n$$, since $$q \mid n! - 1$$ then $$q \le n! - 1 \lt n!$$ so that $n \lt q \lt n!$
n Minus 1 Factorial is Divisible by n
Let $$n \in \mathbb{ N } _ 4$$, show that if $$n$$ is not a prime then we have $\left( n - 1 \right) ! \equiv \begin{cases} 2 \pmod{ n } &\text{ if } n = 4, \\ 0 \pmod{ n } &\text{ if } n \ge 5 \end{cases}$

In the case that $$n = 4$$ then we know that $$\left( n - 1 \right) ! = 6$$ which has remainder 2 upon division by $$4$$ as needed.

On the other hand, whenever we have $$n \ge 5$$ then since $$n$$ is not prime, then it is composite and therefore there exists $$a, b \in \left[ 2, \ldots , n - 1 \right]$$ such that $$n = ab$$. If $$a \neq b$$ then $\left( n - 1 \right) ! = \left( n - 1 \right) \cdots a \cdots b \cdots 2 \cdot 1$ therefore $$n = ab \mid \left( n - 1 \right) !$$ so that $$\left( n - 1 \right) ! \equiv 0 \pmod{ n }$$.

On the other hand, supposing that $$a = b$$ then we see that $$n = a ^ 2$$ since $$a \ge 2$$ then we know that $$a ^ 2 \ge 2a$$. Actually we know more, since $$n \ge 5$$ then $$a \gt 2$$ so we can say that $$a ^ 2 \gt 2a$$ so that $$n \gt 2a$$ so that $$a, 2a$$ both appear in the factorials product so that $$a ^ 2 \mid \left( n - 1 \right) !$$ as needed.

There are Gaps of Arbitrary Size between Consecutive Primes
Suppose that $$\left( p _ 1, p _ 2, p _ 2 , \ldots \right)$$ is an increasing sequence of all the primes, then for every $$m \in \mathbb{ N } _ 1$$ there exists an $$N \in \mathbb{ N } _ 1$$ such that $p _ { N + 1 } - p _ { N } \ge m$
Let $$m \in \mathbb{ N } _ 1$$, consider the following numbers $\left( m + 1 \right) ! + 2, \left( m + 1 \right) ! + 3, \ldots , \left( m + 1 \right) ! + \left( m + 1 \right)$ since there are $$\left( m + 1 \right) - 2 + 1 = m$$ numbers here, and for each element of the form $$\left( m + 1 \right) ! + i$$ we know that $$i \mid \left( m + 1 \right)! + i$$ since $$i \in \mathbb{ N } _ 2$$ this makes it composite, and thus not prime, thus take $$N$$ as the greatest index such that $$p _ N \lt \left( m + 1 \right) ! + 2$$ thus the gap between $$p _ N$$ and $$p _ { N + 1 }$$ must be at least $$m$$ as needed.
Fundamental Theorem of Algebra
For every $$n \in \mathbb{ N } _ 2$$ can be written uniquely as $n = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { \alpha _ i }$ $$\alpha _ i \in \mathbb{ N } _ 1$$ where all but finitely many $$\alpha _ i = 0$$
Prime Factors
Let $$n \in \mathbb{ N } _ 2$$ and consider its prime factorization $n = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { \alpha _ i }$ $$\alpha _ i \in \mathbb{ N } _ 1$$ where all but finitely many $$\alpha _ i = 0$$ then we define $\operatorname{ pf } \left( n \right) = \left\{ p _ i : \alpha _ i \ge 1 \right\}$
Prime Power Sequence
Given any $$n \in \mathbb{ N } _ 1$$ and its prime factorization $$\prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { a _ i }$$ then we define $\operatorname{ pps } \left( n \right) := \left( a _ 1, a _ 2, a _ 3, \ldots \right)$ Note that $$\operatorname{ pps } : \mathbb{ N } _ 2 \to \left\{ \left( x _ n \right): \mathbb{ N } _ 1 \to \mathbb{ N } _ 0 \right\}$$

Note that since each number has a unique factorization then $$\operatorname{ pps }$$ is a bijection, and so $$\operatorname{ pps } ^ { - 1 }$$ exists.

Products as the Addition of Power Sequences
Suppose that $$a, b \in \mathbb{ N } _ 2$$ then $\operatorname{ pps } \left( ab \right) = \operatorname{ pps } \left( a \right) + \operatorname{ pps } \left( b \right)$
Max of the Prime Power Sequence is 0 iff They share no Prime Factors
Let $$a, b \in \mathbb{ N } _ 2$$ then $\operatorname{ pf } \left( a \right) \cap \operatorname{ pf } \left( b \right) = \emptyset \iff \max \left( \operatorname{ pps } \left( a \right), \operatorname{ pps } \left( b \right) \right) = 0$
Relatively Prime In terms of Prime Factorization
The following are equivalent
1. $$\gcd \left( a, b \right) = 1$$
2. $$\max \left( \operatorname{ pps } \left( a \right), \operatorname{ pps } \left( b \right) \right) = 0$$
3. $$\operatorname{ pf } \left( a \right) \cap \operatorname{ pf } \left( b \right) = \emptyset$$
Coprime and 5
Suppose that $$a, b \in \mathbb{ Z }$$ are coprime, then find all possible values of $\gcd \left( a + b, 5 ab \right)$

Let's consider a common divisor $$d$$ of $$a + b$$ and $$5 ab$$ recall that there exists a $$p \in \mathbb{ P }$$ such that $$p \mid d$$ therefore by transitivity we have $$p \mid a + b$$ and $$p \mid 5 ab$$ in the latter case, we see that either $$p \mid 5$$ or $$p \mid a$$ or $$p \mid b$$.

If $$p \mid a$$ then we know that $$p \mid b$$ but then $$\gcd \left( a, b \right) \neq 1$$ which would be a contradiction, therefore $$p \nmid a$$, but symmetrically $$p \nmid b$$ therefore we must have that $$p \mid 5$$ therefore $$p = 5$$

The above says that given any prime divisor of $$d$$ then it turns out that prime divisor must be $$5$$ therefore $$d = 5 ^ k$$ for some $$k \in \mathbb{ N } _ 1$$. Suppose that $$k \ge 2$$, if that's true then we have $$5 ^ k \mid 5 ab$$ and $$5 ^ k \mid a + b$$ from the former so we have $$5 ^ { k - 1 } \mid ab$$ therefore $$5 ^ { k - 1 } \mid a$$ or $$5 ^ { k - 1 } \mid b$$, without loss of generality if we assume that $$5 ^ { k - 1 } \mid a$$ then in conjunction with $$5 ^ k \mid a + b$$ we have $j 5 ^ k = a + b = 5 ^ { k - 1 } l + b$ therefore $b = 5 ^ { k - 1 } \left( j5 - l \right)$ so that $$5 ^ { k - 1 } \mid b$$ therefore $$\gcd \left( a, b \right) \ge 5 ^ { k - 1 }$$ which is a contradiction, therefore it must be that $$k = 1$$.

Therefore the only possible values of $$d$$ is $$1$$ or $$5$$ therefore the only possible values of $$\gcd \left( a + b, 5 ab \right)$$ is $$1$$ or $$5$$

Subsequence of the sum of Two Disjoint Power Sequences Is Still a Disjoint Sum
Suppose that $$\max \left( \operatorname{ pps } \left( a \right) , \operatorname{ pps } \left( b \right) \right) = 0$$ and that $$\left( x _ n \right) \le \operatorname{ pps } \left( a \right) + \operatorname{ pps } \left( b \right)$$ then $\left( x _ n \right) = \left( a _ n \right) + \left( b _ n \right)$ where $$\left( a _ n \right) \le \operatorname{ pps } \left( a \right)$$ and $$\left( b _ n \right) \le \operatorname{ pps } \left( b \right)$$ and $$\max \left( \left( a _ n \right), \left( b _ n \right) \right) = 0$$
A Number Divides a Coprime Product iff It is the product of Individual Divisors
Suppose that $$\gcd \left( a, b \right) = 1$$ then $$c \mid a b$$ if and only if $$c = d _ a d _ b$$ where $$d _ a, d _ b$$ are divisors of $$a, b$$ respectively

$$\implies$$ Suppose that $$c \mid ab$$ therefore $$\operatorname{ pps } \left( c \right) \le \operatorname{ pps } \left( ab \right) = \operatorname{ pps } \left( a \right) + \operatorname{ pps } \left( b \right)$$ and therefore $$\operatorname{ pps } \left( c \right) = \left( a _ n \right) + \left( b _ n \right)$$ where $$\left( a _ n \right) \le \operatorname{ pps } \left( a \right)$$ and $$\left( b _ n \right) \le \operatorname{ pps } \left( b \right)$$ where $$\max \left( \left( a _ n \right), \left( b _ n \right) \right) = 0$$ (by the corollary). Therefore $$c = \operatorname{ pps } ^ { - 1 } \left( a _ n \right) \operatorname{ pps } ^ { - 1 } \left( b _ n \right)$$ but we know that $$\operatorname{ pps } ^ { - 1 } \left( a _ n \right) \mid a$$ and $$\operatorname{ pps } ^ { - 1 } \left( b _ n \right) \mid b$$ as needed.

$$\impliedby$$ Suppose that $$c = d _ a d _ b$$, since they are divisors we know that there exist $$k _ a, k _ b \in \mathbb{ Z }$$ such that $$a = k _ a d _ a$$ and $$b = k _ b d _ b$$ so then $$a b = k _ a k _ b d _ a d _ b = k _ a k _ b c$$ therefore $$c \mid ab$$ as needed.

Euclid
For any $$a, b, p \in \mathbb{ N } _ 1$$ then if $$p$$ is prime, we have $p \mid a b \implies \left( \left( p \mid a \right) \lor \left( p \mid b \right) \right)$
If a Prime Divides a Product it Divides at Least One of the Factors
Suppose that $$a _ 1, \ldots a _ n \in \mathbb{ N } _ 1$$ and $p \mid \prod _ { i = 1 } ^ n a _ i$ then there exists a $$k \in \left[ 1, \ldots , n \right]$$ such that $$p \mid a _ k$$
Prime Divisors of a Factorial are Bounded by the Argument
For every $$p \in \mathbb{ P }$$ and $$n \in \mathbb{ N } _ 2$$ such that $$p \mid n!$$ then $p \le n$
Since $$p \mid 1 \cdots n$$ then there exists some $$k \in \left[ 1, \ldots , n \right]$$ such that $$p \mid k$$ therefore $$p \le k \le n$$ so that $$p \le n$$
A Divisor of a Product of Primes is a Product of Primes with Smaller Powers
Suppose that dm
GCD and LCM as Prime Powers
Suppose that $$P = \left\{ p _ 1, \ldots p _ k \right\}$$ is a collection of primes such that $a = \prod _ { p _ i \in P } p _ i ^ { \alpha _ i } \text{ and } b = \prod _ { p _ i \in P } p _ i ^ { \beta _ i }$ where $$\alpha _ i, \beta _ i \in \mathbb{ N } _ 0$$, then $\gcd \left( a, b \right) = \prod _ { p _ i \in P } p _ i ^ { \min \left( \alpha _ i, \beta _ i \right) } \text{ and } \operatorname{ lcm } \left( a, b \right) = \prod _ { p _ i \in P } p _ i ^ { \max \left( \alpha _ i, \beta _ i \right) }$
Perfect Square iff Every Power in the Prime Factorization is Even
Let $$n \in \mathbb{ N } _ 1$$ then $$n$$ is a perfect square if and only if $n = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { 2 \alpha _ i }$

$$\implies$$ Suppose that $$n$$ is a perfect square therefore $$n = m ^ 2$$ for some $$m \in \mathbb{ Z }$$ but we know that $$m = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { \beta _ i }$$ therefore $$n = \left( \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { \beta _ i } \right) ^ 2 = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { 2 \beta _ 2 }$$

$$\impliedby$$ We have that $$n = \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { 2 \alpha _ i } = \left( \prod _ { i \in \mathbb{ N } _ 1 } p _ i ^ { \alpha _ i } \right) ^ 2$$ therefore $$n$$ is a perfect square.

Division and Prime Power Equivalence
Suppose that $$a, d \in \mathbb{ N } _ 2$$ then $d \mid a \iff \operatorname{ pps } \left( d \right) \le \operatorname{ pps } \left( a \right)$
Every Composite Three Digit Number must have a Prime Factor Less than or Equal to 31
As per title.
Assume that for the sake of contradiction the statement was not true, therefore there is a three digit number such that all of its prime factors are greater than 31, the next prime is $$37$$ and so the smallest three digit number hvaing all prime factors greater than $$31$$ would be $$37 ^ 2 = 1369$$ which is a four digit number, thus our assumption was false, so that every three digit number must have a prime factor less than or equal to $$31$$.
Lower Bound on Primes in Factorization Limits the Number of Prime Factors
Let $$n \in \mathbb{ N } _ 2$$ and suppose that $$p \nmid n$$ for all primes $$p \le \sqrt[3]{ n }$$, what can we conclude about the prime factorizatio of $$n$$ ?
Suppose that $$n = \prod _ { i = 1 } ^ k p _ i ^ { \alpha _ i }$$, we can see that $$p _ i \mid n$$ and therefore by the contrapositive of our assumption we have that $$p _ i \ge \sqrt[3]{ n }$$ therefore $$k \le 2$$ for if $$k \ge 3$$ then \begin{align} n &= \prod _ { i = 1 } ^ { k } p _ i ^ { \alpha _ i } \\ &\ge \prod _ { i = 1 } ^ { k } p _ i \\ &\ge \prod _ { i = 1 } ^ { 3 } p _ i \\ &\gt \sqrt[3]{ n } \sqrt[3]{ n } \sqrt[3]{ n } \\ &= n \end{align} so we've shown that $$n \gt n$$ which is a contradiction so indeed $$k \le 2$$, therefore we conclude that the prime factorization of $$n$$ has at most two different primes in it.
Goldbach Equivalence
Show that every even integer $$\gt 2$$ is the sum of two primes if and only if every integer $$\gt 5$$ is the sum of three primes

$$\implies$$ Suppose the former, let $$n \in \mathbb{ N } _ 1$$ such that $$n \gt 5$$ if $$n$$ is even, then so is $$n - 2$$ and we see $$n - 2 \gt 2$$ and so $$n - 2 = p _ 1 + p _ 2 \iff n = p _ 1 + p _ 2 + 2$$, otherwise use $$3$$.

$$\impliedby$$ Suppose the latter and let $$n \gt 2$$ be even, in other words $$n \ge 4 \gt 3$$ therefore $$n + 2$$ is an even number and we know that $$n + 2 \gt 5$$ therefore $$n + 2 = p _ 1 + p _ 2 + p _ 3$$, since $$n + 2$$ is even then so is $$p _ 1 + p _ 2 + p _ 3$$ but the sum of three numbers is only even when exactly one is even or all are even, therefore at least one of them is even, but there is only one even prime number, and therefore without loss of generality assume that $$p _ 1 = 2$$ so we have $$n + 2 = 2 + p _ 2 + p _ 3$$ and therefore $$n = p _ 2 + p _ 3$$ as needed.

p Minus one Factorial is Congruent to Minus one Mod p
Suppose that $$p \in \mathbb{ N } _ 2$$ is prime, then $\left( p - 1 \right) ! \equiv -1 \left( \operatorname{ mod } p \right)$

Recall that $$S = \left\{ 0, \ldots , p - 1 \right\}$$ is a complete set of residues mod $$p$$, and also for any $$s \in S$$ since $$\gcd \left( s, p \right) = 1$$ then the collection $$T = \left\{ 0, s, \ldots s \left( p - 1 \right) \right\}$$ is a complete system of residues mod $$p$$. Since it is then there is exactly one element $$j \in T$$ such that $$j \equiv 1 \left( \operatorname{ mod } p \right)$$ specifically $$j = s t$$ where $$t \in S$$ so we can say that $$st \equiv 1 \left( \operatorname{ mod } p \right)$$.

In the previous paragraph we've shown the existance of a function $$f : S \to S$$ such that $$x f \left( x \right) \equiv 1 \left( \operatorname{ mod } p \right)$$. We're going to prove this function is injective, that is we will prove that if $$x, y \in S$$ then $$f \left( x \right) = f \left( y \right) \implies x = y$$. Suppose that $$f \left( x \right) = f \left( y \right)$$ therfore $$f \left( x \right) \equiv f \left( y \right) \left( \operatorname{ mod } p \right)$$ moreover by the definition of $$f$$ we know that $$x f \left( x \right) \equiv 1 \equiv y f \left( y \right) \left( \operatorname{ mod } p \right)$$ therefore we have $x f \left( x \right) \equiv y f \left( y \right) \equiv y f \left( x \right) \left( \operatorname{ mod } p \right)$ thus since $$f \left( x \right) \in S$$ we know that $$\gcd \left( f \left( x \right), p \right) = 1$$ therefore $$x \equiv y \left( \operatorname{ mod } \frac{p}{1} \right)$$ so that $$x = y$$ as needed. As a side note since the size of the domain is the same as the size of the range, then we know that $$f$$ is a bijection.

For a moment let's consider the case when $$f \left( x \right) = x$$ if that were true we have $$x f \left( x \right) \equiv x ^ 2 \equiv 1 \left( \operatorname{ mod } p \right)$$ therefore $$x \equiv \pm 1 \left( \operatorname{ mod } p \right)$$ if $$x \equiv 1 \left( \operatorname{ mod } p \right)$$ this means that $$p \mid x - 1$$, if $$x - 1 \neq 0$$ then we know that $$p \le \left\lvert x - 1 \right\rvert$$, since $$x \in S$$ then $$x - 1 \in \left\{ -1, \ldots p - 2 \right\}$$ so that $$\left\lvert x - 1 \right\rvert \le p - 2$$ but then we deduced that $$p \le p - 2$$ which is a contradiction so that we must have $$x - 1 = 0$$ so that $$x = 1$$. On the other hand if we know that $$x \equiv -1 \left( \operatorname{ mod } p \right)$$ then we see that $$p \mid x + 1$$ since $$x + 1 \in \left\{ 1, \ldots , p \right\}$$ then the only possibility is that $$x + 1 = p$$ which is that $$x = p - 1$$.

The result of the above paragraph is that if $$f \left( x \right) = x$$ then the only possibility is that $$x = 1$$ or that $$x = p - 1$$, so $$x \in \left\{ 2, \ldots , p - 2 \right\} \implies f \left( x \right) \neq x$$ and $$f \left( x \right) \in \left\{ 2, \ldots , p - 2 \right\}$$, and this function is a bijection, remaining a bijection on the restriction of the domain and range to $$\left\{ 2, \ldots , p - 2 \right\}$$.

For a moment note that if $$p = 2$$ this whole question is trivial because $$\left( 2 - 1 \right) ! \equiv -1 \left( \operatorname{ mod } 2 \right)$$, therefore we can assume that $$p \ge 3$$ also this implies that $$p$$ is odd, therefore when we consider the collection $K = \left\{ 2, \ldots , p - 2 \right\}$ then since $$\left\lvert K \right\rvert = p - 2 - 2 + 1 = p - 3$$ then the size of $$K$$ is even since $$p$$ is odd. Let $$k _ 1 \in K$$ then $$f \left( k _ 1 \right) \neq k$$, Let $$K _ 1 = K \setminus \left\{ k _ 1, f \left( k _ 1 \right) \right\}$$, since $$f$$ is a bijection, then by repeating this process $$l = \frac{\left\lvert K \right\rvert }{2}$$ times then $$K _ l = \emptyset$$ which is to say that $$\left\{ \left\{ k _ i, f \left( k _ i \right) \right\}: i \in \left[ 1, \ldots, l \right] \right\}$$ is a partition of $$K$$, thus $2 \cdots \left( p - 2 \right) = \prod _ { i = 1 } ^ { l } k _ i f \left( k _ i \right)$ But we see $$\prod _ { i = 1 } ^ { l } k _ i f \left( k _ i \right) \equiv \prod _ { i = 1 } ^ { l } 1 \equiv 1 \left( \operatorname{ mod } p \right)$$ and therefore $$2 \cdots \left( p - 2 \right) \equiv 1 \left( \operatorname{ mod } p \right)$$ therefore $\left( p - 1 \right) ! \equiv 2 \cdots \left( p - 2 \right) \left( p - 1 \right) \equiv 1 \left( p - 1 \right) \equiv -1 \left( \operatorname{ mod } p \right)$ as needed.

The above is usually referred to "Wilsons Theorem".

p Minus two Factorial is Congruent to One Mod p
Suppose that $$p \in \mathbb{ N } _ 2$$ is prime, then $\left( p - 2 \right) ! \equiv 1 \pmod{ p }$
Recall that $$\left( p - 1 \right) ! \equiv -1 \pmod{ p }$$ but at the same time we know that $$\left( p - 1 \right)! \equiv \left( p - 1 \right) \left( p - 2 \right) ! \equiv -1 \left( p - 2 \right) !$$ therefore multiplying both sides by $$-1$$ we obtain that $\left( p - 2 \right) ! \equiv 1 \pmod{ p }$

This allows us to conclude things like: $$15! \equiv 1 \pmod{ 17 }$$.

Factorial Fun
Find the remainder when $$4 \left( 29! \right) + 5!$$ is divided by $$31$$
We know that $$29! \equiv 1 \pmod{ 31 }$$ and that $$5! = 120$$ therefore $4 \left( 29! \right) + 5! \equiv 124 \equiv 4 \left( 31 \right) \equiv 0 \pmod{ 31 }$
1 2 3 n go
Show that for any $$n \in \mathbb{ N } _ 2$$ then $n \mid 2 \left( n - 3 \right) ! + 1$ iff $$n$$ is prime
Suppose that $$n$$ is prime, therefore $$\left( n - 2 \right) ! \equiv 1 \pmod{ n }$$ we also know that $$n - 2 \equiv -2 \pmod{ n }$$ therefore we know that $\left( n - 3 \right) ! \left( -2 \right) \equiv 1 \pmod{ n } \iff 2 \left( n - 3 \right) ! \equiv -1 \pmod{ n }$ thus $2 \left( n - 3 \right) ! + 1 \equiv 0 \pmod{ n }$ as needed.

For the other direction assume that $$n \mid 2 \left( n - 3 \right) ! + 1$$ therefore we know that $2 \left( n - 3 \right) ! \equiv -1 \pmod{ n }$ by multiplying both sides by -1, re-writing the factorial and then multiplying by -1 again, we obtain that $\left( n - 1 \right) ! \equiv -1 \pmod{ n }$ therefore by wilsons theorem we know that $$n$$ is prime.

Generalized Wilson's Theorem
Suppose that $$p \in \mathbb{ P }$$ and that $$a \in \left[ 1, \ldots , p \right]$$ then we have $\left( a - 1 \right) ! \left( p - a \right) ! \equiv \left( -1 \right) ^ a$
We know that the base case holds. Therefore we move directly to the induction step, where we assume that the statement holds true for $$k \in \mathbb{ N } _ 1$$, and we have to prove that $\left( k \right) ! \left( p - k - 1 \right) ! \equiv \left( -1 \right) ^ { k + 1 }$ We have that \begin{align} \left( -1 \right) ^ k &\equiv \left( k - 1 \right) ! \left( p - k \right) ! \\ & \equiv \left( k - 1 \right) ! \left( p - k \right) \left( p - k - 1 \right) ! \\ & \equiv \left( k - 1 \right) ! \left( -k \right) \left( p - k - 1 \right) ! \\ & \equiv \left( -1 \right) k! \left( p - k - 1 \right) ! \\ \end{align} thus by multiplying both sides by $$-1$$ we obtain that $k! \left( p - k - 1 \right) \equiv \left( -1 \right) { k + 1 }$ as needed.
Wilson Folded in Half
Suppose that $$p \in \mathbb{ N } _ 3$$ is prime, then $\left[\left(\frac{p-1}{2}\right)!\right]^{2}\equiv (-1)^{\frac{p+1}{2}} \pmod{p}$
Observe that $$\frac{p - 1}{2}, \frac{p + 1 }{2} \in \mathbb{ N } _ 1$$ since $$p$$ is odd. Since $$1 \lt p$$ then we know $$2 \lt p + 1 \lt 2p$$ so that $$1 \lt \frac{p + 1 }{2} \lt p$$, similarly since $$3 \le p$$ then $$2 \le p - 1 \lt p$$ so that $$1 \le \frac{p - 1}{2} \lt \frac{p}{2} \lt p$$, moreover $$\frac{p - 1}{2} + 1 = \frac{p + 1 }{2}$$ therefore we have: $\left( p - 1 \right) ! = 1 \cdot 2 \cdots \left( \frac{p - 1}{2} \right) \cdot \left( \frac{p + 1 }{2} \right) \cdots \left( p - 1 \right)$

focusing on the terms that come after $$\frac{p - 1 }{2}$$ in the above product we see \begin{align*} \frac{p+1}{2}&\equiv -\frac{p-1}{2}\pmod{p} \\ &\vdots\\ p-2&\equiv -2\pmod p\\ p-1&\equiv -1\pmod p \end{align*} that first congruence comes from the fact that $$\frac{p + 1 }{2} = p - \frac{p - 1}{2}$$, and notice that they are congruent to the negative versions of the terms in the first half of the factorial, and so we can "fold" it in half to get $\left( p - 1 \right) ! \equiv 1 \cdot \left( - 1 \right) \cdot 2 \cdot \left( -2 \right) \cdots \left( \frac{p - 1}{2} \right) \left( - \frac{p - 1}{2} \right) \pmod{p}$ Since there are exactly $$\frac{p - 1}{2}$$ negative terms we can factor a $$-1$$ out from all of them, leaving square of the consecutive numbers so that $\left( p - 1 \right) ! \equiv \left( -1 \right) ^ { \frac{p - 1}{2} } \left( 1 \cdot 2 \cdots \frac{p - 1}{2} \right) ^ 2 \pmod{ p }$ by wilsons theorem we know that $$\left( p - 1 \right) ! \equiv -1 \pmod{ p }$$ therefore we have $-1 \equiv \left( -1 \right) ^ { \frac{p - 1}{2} } \left[ \left( \frac{p - 1}{2} \right) ! \right] ^ 2 \pmod{ p }$ since $$p$$ is odd, then $$p - 1$$ is even, therefore $$\left( -1 \right) ^ { p - 1 } = 1$$, therefore \begin{align} -1 \left( -1 \right) ^ { \frac{p - 1}{2} } &\equiv \left( -1 \right) ^ { \frac{p - 1}{2} } \left( -1 \right) ^ { \frac{p - 1}{2} }\left[ \left( \frac{p - 1}{2} \right) ! \right] ^ 2 \pmod{ p }\\ \left( -1 \right) ^ { \frac{p - 1}{2} + \frac{2}{2} } &\equiv \left( -1 \right) ^ { p - 1 } \left[ \left( \frac{p - 1}{2} \right) ! \right] ^ 2 \pmod{ p }\\ \left( -1 \right) ^ { \frac{p + 1 }{2} } &\equiv \left[ \left( \frac{p - 1}{2} \right) ! \right] ^ 2 \pmod{ p } \end{align} as needed.

Fermats Helper
Suppose $$p \in \mathbb{ N } _ 2$$ is prime, and $$a \in \mathbb{ Z }$$ so that $$p \nmid a$$ then $a ^ { p - 1 } \equiv 1 \left( \operatorname{ mod } p \right)$
Recall that $$R = \left\{ 0, \ldots , p - 1 \right\}$$ is a complete system or resides mod $$p$$, and since $$\gcd \left( p, a \right) = 1$$ then so is $$A = \left\{ 0, \ldots , a \left( p - 1 \right) \right\}$$ therefore $\prod _ { x \in A \setminus \left\{ 0 \right\} } x \equiv \left( p - 1 \right) ! \left( \operatorname{ mod } p \right)$ But we also know that $$\prod _ { x \in A \setminus \left\{ 0 \right\} } x = a ^ { p - 1 } \left( p - 1 \right) !$$ so we conclude that $\left( p - 1 \right)! a ^ { p - 1 } \equiv \left( p - 1 \right) ! \left( \operatorname{ mod } p \right)$ But then $a ^ { p - 1 } \equiv 1 \left( \operatorname{ mod } p \right)$
Fermats Helper Mod Exponent
Suppose that $$p \in \mathbb{ N } _ 2$$ is prime, and $$a, b \in \mathbb{ Z }$$ such that $$p \nmid a$$ then $a ^ b \equiv a ^ { b ~\%~ \left( p - 1 \right) } \pmod{ p }$
By the quotient remainder theorem we know that $$b = q \left( p - 1 \right) + r$$ where $$r = b ~\%~ \left( p - 1 \right)$$ therefore \begin{align} a ^ b &\equiv a ^ { q \left( p - 1 \right) + r } \\ &\equiv a ^ { q \left( p - 1 \right) } a ^ r \\ &\equiv \left( a ^ { p - 1 } \right) ^ q a ^ r \\ &\equiv 1 ^ q a ^ r \\ &\equiv a ^ r \equiv a ^ { b ~\%~ \left( p - 1 \right) } \pmod{ p } \end{align} where in the 3rd last line we used fermats helper.
Fermats Little
For any $$p \in \mathbb{ N } _ 2$$ which is prime, then for any $$a \in \mathbb{ Z }$$ we have $a ^ p \equiv a \left( \operatorname{ mod } p \right)$

Suppose $$p \mid a$$ then the statement holds true as $$0 ^ p \equiv 0 \left( \operatorname{ mod } p \right)$$. On the other hand if $$p \nmid a$$ then $$a ^ { p - 1 } \equiv 1 \left( \operatorname{ mod } p \right)$$ so by multiplying by $$a$$ we have $a ^ p \equiv a \left( \operatorname{ mod } p \right)$

Remainder of Power Plus One
Find the remainder of $$11 ^ { 104 } + 1$$ modulo $$17$$
Observe that by fermats helper since $$17 \nmid 11$$ we have that $$11 ^ { 16 } \equiv 1 \pmod{ 17 }$$ since $$104 = 16 \cdot 6 + 8$$ then we see that $11 ^ { 104 } \equiv \left( 11 ^ 16 \right) ^ 6 \left( 11 \right) ^ 8 \equiv 11 ^ 8 \pmod{ 17 }$ since $$11 ^ 2 = 121 = 17 \cdot 7 + 2$$ then $$11 ^ 8 \equiv \left( 11 ^ 2 \right) ^ 4 \equiv 16 \equiv -1 \pmod{ 17 }$$ therefore $11 ^ { 104 } + 1 \equiv 0 \pmod{ 17 }$
2's and 5's
Show that $$2222 ^ { 5555 } + 5555 ^ { 2222 }$$ is divisible by $$7$$

Note that in general $$AB \cdot 0101 \ldots 01 = ABAB \ldots AB$$, therfore note that $$5555 = 101 \cdot 55$$ and that $$2222 = 101 \cdot 22$$.

We use this to simplify each base mod $$7$$, we have that \begin{align} 5555 &\equiv 101 \cdot 55\\ &\equiv \left( 7 \cdot 14 + 3 \right) \cdot \left( 7 \cdot 7 + 6 \right) \\ &\equiv -3 \pmod{ 7 } \end{align}

also that \begin{align} 2222 &\equiv 101 \cdot 22 \\ &\equiv 3 \cdot \left( 7 \cdot 3 + 1 \right) \\ &\equiv 3 \end{align} therefore we know that $2222 ^ { 5555 } + 5555 ^ { 2222 } \equiv \left( -3 \right) ^ { 5555 } + 3 ^ { 2222 } \pmod{ 7 }$ We will now look at $$2222 ~\%~ 6$$ since we know that $$2220 ~\%~ 6 = 0$$ then $$2222 ~\%~ 6 = 2$$, we will now look at $$5555 ~\%~ 6$$ since we know that that $$5550 ~\%~ 6 = 0$$ then $$5555 ~\%~ 6 = 5$$ therefore $\left( -3 \right) ^ { 5555 } + 3 ^ { 2222 } \equiv \left( -3 \right) ^ 2 + 3 ^ { -1 } \equiv 5 + 2 \equiv 0 \pmod{ 7 }$

Prime Power Sum
Suppose that $$p \in \mathbb{ P } _ 3$$ then compute $\left( 1 ^ p + 2 ^ p + 3 ^ p + \cdots + \left( p - 1 \right) ^ p \right) ~\%~ p$
Since for each $$x \in \left[ 1, \ldots , p - 1 \right]$$ we know that by fermats little theorem that $x ^ p \equiv x \pmod{ p }$ thus we have that $\sum _ { i = 1 } ^ { p - 1 } i ^ p \equiv \sum _ { i = 1 } ^ { p - 1 } i \equiv \frac{\left( p - 1 \right) p }{2} \pmod{ p }$ now since $$p \in \mathbb{ P } _ 3$$ then we know that $$p - 1$$ is an even number therefore we know that $$\frac{ p - 1}{2} \in \mathbb{ Z }$$ therefore we have $\frac{\left( p - 1 \right) p }{2} \equiv \frac{p - 1}{2} p \equiv 0 \pmod{ p }$ which shows that $\left( 1 ^ p + 2 ^ p + 3 ^ p + \cdots + \left( p - 1 \right) ^ p \right) ~\%~ p = 0$
If a Doesn't Divide p Then Fermats Little Theorem and Fermats Little Helper are Equivalent
Suppose that $$p \in \mathbb{ N } _ 2$$ is prime and $$a \in \mathbb{ Z }$$ such that $$p \nmid a$$ then $a ^ { p - 1 } \equiv 1 \iff a ^ p \equiv a \left( \operatorname{ mod } p \right)$
$$\implies$$ Suppose that $$a ^ { p - 1 } \equiv 1$$ then multiply both sides by $$a$$. $$\impliedby$$ Suppose that $$a ^ p \equiv a$$ since $$\gcd \left( a, p \right) = 1$$ then $$a ^ { p - 1 } \equiv 1 \left( \operatorname{ mod } p \right)$$ as needed.
Almost Prime
Show that for any $$a \in \mathbb{ Z }$$ we have $a ^ 9 \equiv a \left( \operatorname{ mod } 30 \right)$

Recall that $$30 = 2 \cdot 3 \cdot 5$$, what we will is that we will consider the equations $$x \equiv a \left( \operatorname{ mod } 2, 3, 5 \right)$$, trivially for each equation $$x = a$$ works, and therefore by the chinese remainder theorem any other solution is congruent to $$a$$ mod 30. Our goal will be to show that $$a ^ 9$$ also satisfies the equations which will allow us to deduce that $a ^ 9 \equiv a \left( \operatorname{ mod } 30 \right)$

Lets confirm that $$a ^ 9 \equiv a \left( \operatorname{ mod } 2 \right)$$, we do know that $$a ^ 2 \equiv a \left( \operatorname{ mod } 2 \right)$$ so that $$a ^ 9 \equiv \left( a ^ 2 \right) ^ 4 a \equiv 1 a \equiv a$$ as needed. We take a similar approach to $$a ^ 9$$ mod $$3$$ since we know that $$a ^ 3 \equiv a \left( \operatorname{ mod } 3 \right)$$ then $$a ^ 9 = \left( a ^ 3 \right) ^ 3 \equiv a$$, finally for $$a ^ 9$$ mod $$5$$ we have $$a ^ 9 = a ^ 5 a ^ 4 \equiv a a ^ 4 \equiv a ^ 5 \equiv a$$ as needed.

Therefore we've deduced that $$x = a ^ 9$$ is also a solution, and therefore $a ^ 9 \equiv a \left( \operatorname{ mod } 30 \right)$

Carmichael
We say that $$n \in \mathbb{ N } _ 1$$ is carmichael if $$a ^ n \equiv a \left( \operatorname{ mod } n \right)$$ for every $$a \in \mathbb{ Z }$$, and $$n$$ is not a prime.
561 is Carmichael
As per title.

Our goal is to show that for any $$a \in \mathbb{ Z }$$ we have $$a ^ { 561 } \equiv a \left( \operatorname{ mod } 561 \right)$$, we also have to show that $$n$$ is not a prime, which is true because $$561 = 3 \cdot 11 \cdot 17$$. Refocusing on the initial goal, we will prove this is true by considering the equations $$x \equiv a \left( \operatorname{ mod } 11, 13, 17 \right)$$ and noting that $$x = a$$ is a solution, and also showing that $$x = a ^ { 561 }$$is also a solution, and therefore concluding $a ^ { 561 } \equiv a \left( \operatorname{ mod } 3 \cdot 11 \cdot 17 \right)$ by the chinese remainder theorem.

Note that if $$d \mid a$$ then $$d \mid a ^ { 561 }$$ and therefore $$a ^ { 561 } \equiv a \left( \operatorname{ mod } d \right)$$ therefore applying that idea to $$d = 3, 11, 17$$ we see that if any of them divide $$a$$ we immediately obtain $$a ^ { 561 } \equiv a \left( \operatorname{ mod } d \right)$$.

Otherwise things still work, for if $$3 \nmid a$$ then we know that $$a ^ 2 \equiv 1 \left( \operatorname{ mod } 3 \right)$$ since $$560$$ is even then $$a ^ { 560 } \equiv \left( a ^ 2 \right) ^ { 280 } \equiv 1$$ so we have $$a ^ { 561 } \equiv a \left( \operatorname{ mod } 3 \right)$$. Similarly if $$11 \nmid a$$ then $$a ^ { 10 } \equiv 1 \left( \operatorname{ mod } 11 \right)$$ and therefore $$a ^ { 560 } \equiv 1$$ so that $$a ^ { 561 } \equiv a \left( \operatorname{ mod } 11 \right)$$. Finally if $$17 \nmid a$$ then we have $$a ^ { 16 } \equiv 1 \left( \operatorname{ mod } 17 \right)$$ and $$a ^ { 560 } \equiv \left( a ^ { 16 } \right) ^ { 35 } \equiv 1$$ so that $$a ^ { 561 } \equiv a \left( \operatorname{ mod } 17 \right)$$.

What we've shown is that no matter what we have that $$a ^ 561 \equiv a \left( \operatorname{ mod } 3, 11, 17 \right)$$ and therefore by our original paragraph we are done.

Korselt's Criterion
$$n \in \mathbb{ N } _ 1$$ is Carmichael if and only if $$n$$ is squarefree and for every prime $$p$$ such that $$p \mid n \implies p - 1 \mid n - 1$$.
1729 is Carmichael
As per title.
As it turns out $$1729 = 7 \cdot 13 \cdot 9$$, therefore it is square free and since $$6, 12, 8 \mid 1728$$ then it is carmichael
Fermats Compositeness Test
Suppose $$n, a \in \mathbb{ Z }$$ such that $$\gcd \left( a, n \right) = 1$$ then if $$a ^ { n - 1 } \not \equiv 1 \left( \operatorname{ mod } n \right)$$ then $$n$$ is composite
Observe the contrapositive of fermats little helper which states that $a ^ { p - 1 } \not \equiv 1 \implies \left( p \mid a \lor p \text{ is not prime } \right)$ from this we conclude that $$\left( p \mid a \lor p \text{ is not prime } \right)$$ but since $$\gcd \left( a, n \right) = 1$$ then $$n \nmid a$$ therefore we must conclude that $$n$$ is not prime, and therefore composite.
Fermat Witness
If $$n, a$$ passes the compositeness test, then $$a$$ is said to be a Fermat Witness for $$n$$
Pseudoprime to base $$a$$
Suppose that $$n, a \in \mathbb{ Z }$$ and that $$a ^ { n - 1 } \equiv 1 \left( \operatorname{ mod } n \right)$$ and $$n$$ is not prime, then $$n$$ is called a pseudoprime to base $$a$$.

Most of the time when you find that $$a ^ { n - 1 } \equiv 1 \left( \operatorname{ mod } n \right)$$, then $$n$$ is prime, that's why the ones that aren't are called pseudoprimes.

$$341$$ is a Pseudoprime to base $$2$$
As per title.

Firstly we note that $$341$$ is divisible by $$11$$ because $$3 - 4 + 1 = 0$$ and therefore $$341 = 11 \cdot 31$$ now we'll verify that $$2 ^ { 340 } \equiv 1 \pmod{ 341 }$$, note that $$341 \cdot 3 = 1023$$ and since $$2 ^ 10 = 1024$$ so that $$1024 \equiv 1 \pmod{ 341 }$$ thus we know that $$2 ^ 10 \equiv 1 \pmod{ 341 }$$ therefore $$3 ^ { 340 } \equiv \left( 3 ^ 10 \right) ^ 34 \equiv 1 \pmod{ 341 }$$.

On the other hand $$3 ^ { 341 } \not \equiv 3 \pmod{ 341 }$$, we can see this is true by using the chinese remainder theorem on the equations $$x \equiv 3 \pmod{ 11 }$$ and $$x \equiv 3 \pmod{ 31 }$$ and noting that $$341$$ solves it, but also the switch summation also solves it but the switch is not congruent to 3, so it works.

Fermat Primality Test
Let $$n \in \mathbb{ N } _ 2$$, and define the following function $$f \left( X \right)$$:
1. if $$X = \emptyset$$ return $$T$$
2. let $$a \in X$$
3. if $$a ^ { n - 1 } \not \equiv 1 \left( \operatorname{ mod } n \right)$$ return $$F$$
4. return $$f \left( X \setminus \left\{ a \right\} \right)$$
Let $$G = \left\{ x \in \left[ 1, \ldots, n - 2 \right]: \gcd \left( x, n \right) = 1 \right\}$$. If $$f \left( G \right) = F$$ then $$n$$ is composite, otherwise we say that $$n$$ is a probable prime
Every Carmichael Number is a Probable Prime
Suppose $$n \in \mathbb{ N } _ 1$$ is Carmichael, then using the Fermat Primality Test, it is a probable prime.
Since $$n$$ is Carmichael we know that $$a ^ n \equiv a \left( \operatorname{ mod } n \right)$$ for any $$a \in \mathbb{ Z }$$, during the test each $$a$$ has the property that $$\gcd \left( a, n \right) = 1$$ therefore $$a ^ { n - 1 } \equiv 1 \left( \operatorname{ mod } n \right)$$, this means at each stage in the test line 4 is reached until it terminates at line 1 and returning $$T$$ therefore $$n$$ is a probable prime.
At Least Half of the Tested Elements will be Witnesses to a Composite Non-Carmichael Number
Suppose that $$n \in \mathbb{ N } _ 1$$ is composite and not Carmichael and let $$G = \left\{ g \in \left[ 1, \ldots , n - 1 \right]: \gcd \left( g, n \right) = 1 \right\}$$ and $$W = \left\{ w \in G: w ^ { n - 1 } \not \equiv 1 \left( \operatorname{ mod } n \right) \right\}$$, then $\left\lvert W \right\rvert \ge \frac{\left\lvert G \right\rvert }{2}$

To prove this true, we will consider the set $$L = \left\{ l \in G: l ^ { n - 1 } \equiv 1 \left( \operatorname{ mod } n \right) \right\}$$, and note that $$G = L \sqcup W$$ then if we are able to show that $$\left\lvert L \right\rvert \le \left\lvert W \right\rvert$$ then we have $\left\lvert G \right\rvert \le \left\lvert L \right\rvert + \left\lvert W \right\rvert \le 2 \left\lvert W \right\rvert$ and we would deduce that $$\left\lvert W \right\rvert \ge \frac{\left\lvert G \right\rvert }{2}$$

Since $$L \subseteq \left[ 0, \ldots , n - 1 \right]$$ then all elements are incongruent mod $$n$$, let $$w \in W \subseteq G$$ this can be done because $$W$$ is non-empty as $$n$$ is not carmichael so that there exists an element $$g \in G$$ such that $$g ^ { n - 1 } \not \equiv 1 \left( \operatorname{ mod } n \right)$$.

Since $$\gcd \left( w, n \right) = 1$$ then the collection of elements $$w L = \left\{ w l : l \in L \right\}$$ are also incongruent mod $$n$$, since equality implies congruent, then non-congruent implies not equal, and therefore $$wL$$ generates a collection of unique elements, so that $$\left\lvert L \right\rvert = \left\lvert wL \right\rvert$$.

But note, given some $$wl \in wL$$ we have that $\left( wl \right) ^ { n - 1 } \equiv w ^ { n - 1 } l ^ { n - 1 } \equiv w ^ { n - 1 } \not \equiv 1 \left( \operatorname{ mod } n \right)$ therefore we deduce that $$wL \subseteq W$$ so then $$\left\lvert wL \right\rvert \le L$$ therefore $$\left\lvert L \right\rvert \le \left\lvert W \right\rvert$$, so that by our introductory paragraph, we've proven what we needed to.

Every Odd Number is a Difference of Squares
For any $$n \in \mathbb{ N } _ 1$$ that is odd there exists $$x, y \in \mathbb{ N } _ 1$$ such that $n = x ^ 2 - y ^ 2$
Since $$n$$ is odd, then there is some $$k \in \mathbb{ Z }$$ such that $$n = 2k + 1$$, note that $$2k + 1$$ is similar to $$\left( k + 1 \right) ^ 2 = k ^ 2 + 2k + 1$$ and so that $$2k + 1 = \left( k + 1 \right) ^ 2 - k ^ 2$$ as needed. Note that in this proof $$k = \frac{n - 1}{2}$$ and $$k + 1 = \frac{n + 1 }{2}$$ so we are really saying that $n = \left( \frac{n + 1}{2} \right) ^ 2 + \left( \frac{n - 1}{2} \right) ^ 2$
The Sum is less than or Equal to the Product
Suppose that $$a, b \in \mathbb{ N } _ 2$$ then $a + b \le a \cdot b$ if $$a \lt b$$ or $$b \lt a$$ then $a + b \lt a \cdot b$
Suppose that $$a, b \in \mathbb{ N } _ 2$$ and without loss of generality assume that $$a \le b$$ therefore $a + b \le b + b \le 2b \le ab$ Now suppose that $$a \lt b$$ $a + b \lt b + b = 2b \le ab$
Fermats Factorization
Suppose that $$n \in \mathbb{ N } _ 2$$ is odd, then $$n$$ is composite iff there exists $$x, y \in \mathbb{ N } _ 1$$ such that $n = x ^ 2 - y ^ 2$ where $$x \neq \frac{n + 1 }{2}$$

$$\implies$$ We can't use the fact that every odd number is a difference of squares, this is because the solution to that equation was specifically $$x = \frac{n + 1}{2}$$ which is explicitly disallowed for this proposition, therefore instead we note that $$n$$ is composite so there exists $$c, d \in \left[ 2, \ldots , n - 1 \right]$$ such that $$n = cd$$. But at the same time, we know that \begin{align} \left( \frac{c + d}{2} \right) ^ 2 - \left( \frac{c - d}{2} \right) ^ 2 &= \frac{c ^ 2 + 2 cd + d ^ 2 - c ^ 2 + 2cd - d ^ 2}{4} = cd \end{align} We'll now prove that $$\frac{c + d}{2} \neq \frac{n + 1 }{2}$$.

Since $$c, d \in \mathbb{ N } _ 2$$ then we know that $$c + d \le c \cdot d = n$$ so that $$c + d \lt n + 1$$ therefore $$\frac{c + d}{2} \neq \frac{n + 1 }{2}$$ as needed.

$$\impliedby$$ Suppose that $$n = x ^ 2 - y ^ 2$$ since $$n \ge 2$$ then we know $$x \gt y$$ also note that $$x ^ 2 - y ^ 2 = \left( x + y \right) \left( x - y \right)$$ therefore $$\left( x + y \right) \mid n$$ and $$\left( x - y \right) \mid n$$, if $$n$$ were to be prime, then $$\left( x + y \right), \left( x - y \right) \in \left\{ 1, n \right\}$$ without loss of generality suppose that $$x - y = 1$$ and $$x + y = n$$ therefore $$2x = n + 1$$ so $$x = \frac{n + 1 }{2}$$ then $$y = \frac{n - 1}{2}$$ which is a contradiction, and therefore $$n$$ must be composite.

            
import math

def fermat_factor(n):
assert n % 2 != 0, "Fermat's factorization method works only for odd integers."

print(f"Starting Fermat's factorization for n = {n}")

# Calculate the ceiling of the square root of n
x = math.ceil(math.sqrt(n))
print(f"Initial x (ceiling of the square root of n): {x}")

if x * x == n:
print(f"{n} is a perfect square. Factors are {x} and {x}.")
return x, x

y2 = x * x - n
print(f"Initial y^2 = {x}^2 - {n} = {y2}")

# Loop will always terminate because (x, y) = ((n + 1) / 2, (n - 1) / 2) is a valid solution
while not last_two_digits_possible_square(y2) or not is_perfect_square(y2):
print(f"y^2 = {y2} is not a perfect square. Incrementing y^2 and then x.")

# Update y^2 incrementally
y2 += 2 * x + 1  # Efficiently calculate the next y2 using (x+1)^2 = x^2 + 2x + 1
x += 1  # Increment x after updating y^2

print(f"New x = {x}, new y^2 = {x}^2 - {n} = {y2}")

y = int(math.isqrt(y2))
print(f"Found perfect square y^2 = {y2}. y = sqrt({y2}) = {y}")
print(f"Factors are (x - y) = {x - y} and (x + y) = {x + y}")

return (x - y, x + y)

def last_two_digits_possible_square(n):
# Set of possible last two digits of a perfect square
possible_last_two_digits = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81,
20, 21, 24, 25, 29, 41, 44, 45, 61, 69, 84}
return (n % 100) in possible_last_two_digits

def is_perfect_square(n):
# Check if the number is a perfect square
root = int(math.isqrt(n))
return root * root == n

# Example usage:
n = 5959
factors = fermat_factor(n)
print(f"The factors of {n} are {factors}")