My first version was slow (~1m) because I was checking too many values, but after reading about Sieve of Eratosthenes was able to speed up to <1s.