GNU coreutils has a factor command. If you are using a linux machine it is most likely already installed. On Mac’s you can install coreutils using a package manager like homebrew.
Brute Force
First lets make a Primes object that can generate an array of primes up to $n$. A prime number is any number that is only divisible by 1 and and itself. For a truly naive approach, to check if $n$ is prime, you would check every number in the range of $[2, n)$.
For our naive approach, we will keep an array of primes we have found and check if $n$ is divisible by any prime. We can do this because any non-prime number that $n$ is divisible by, is itself divisible by a previously found prime, making $n$ also divisible by that prime. We will also limit our search to odd numbers.
More Optimizations
The big optimization we can make is only checking if $n$ is divisible by numbers $\le \sqrt n$. This is because if $pq=n$ for $p$ and $q\neq 1$ and $\neq \sqrt n$, then $p$ or $q$ must be $\le \sqrt n$. If both $p$ and $q \gt \sqrt n$ it would contradict $\sqrt n \times \sqrt n = n$.
Another optimization that we can make that reduces the number of $n$’s we need to check is all primes past $3$ can be generalized in the form $6i \pm 1$.
The proof for this generalization can be stated as:
Given a number, $n \gt 3$, dividing $n$ by $6$ gives you:
$n = 6x + r$ ; $x$ is a non-negative integer and $r$ is the remainder
for $r$ is $0$, $2$, or $4$ ; $n$ is divisible by $2$
for $r$ is $3$ ; $n$ is divisible by $3$
for $r$ is $1$ ; $n$ is $1$ more than a multiple of $6$
for $r$ is $5$ ; $n$ is $1$ less than a multiple of $6$
That means for all $n$ not in for form of $6i \pm 1$, $n$ is divisible by $2$ or $3$. This means we can eliminate all those from our checks.
Primes!
Using the Prime Generator
To find the prime factors for our number, we can apply the same logic we did for checking primes for divisibility by only checking up to $\sqrt n$.