I just stored the primes in an array as I found them. To find them you just see if the number can be divided by any of the primes you have already found.