Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Era
Now that you've got a basic sieve in place, note that the inner loop need only continue until temp[i]*temp[i] > prime.
temp[i]*temp[i] > prime