And what about the first point about the limit of the J loop?
I noticed the the code does a lot of trial divisions of non-primes. For example, for 703, it tries
4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26. This will get worse for the bigger numbers.
Division is a relatively slow operation, and when speed matters, you want to avoid doing it unnecessarily.
I’d suggest using a sieve to first get a list of prime numbers, and then doing the trial division with the primes.