I usually do something like what JamesCherrill described. I have a number that I take the square root of. If any integer is less than or equal to that square root, I test for divisibility by 3 (anything divisible by 2 is ignored because 2 is the only even prime, and anything less than 2 is not prime, so I just start a loop at 3 and increment by 2), by 5, by 7, not by 9 because that is already divisible by 3, by 11, 13, not by 15 because 15=3*5, 17, 19, and so on. In other words, I have an array that I append new primes to when they're found. A much longer, but less intensive test is to test against half of a given number rather than the square root of that number. If you need to generate primes between 1 and 10, then you could benefit from n/2 rather than sqrt(n) because a square root can cause issues in some cases if it isn't converted to an integer properly. If you needed something like 1 through 1000000, then I would go the square root route.
I should note that several patterns have been found, but not very many can be applied using algorithms, because the patterns are more visual, like a sort of spiral created by arranging the numbers in a certain formation. One thing you might also consider is the idea that sometimes Mersenne primes are useful. They are prime numbers of the form Math.pow(2, exponent) - 1. In many cases, 2 less or 2 more than that a number using that equation can be prime. For a range like 1 through 10, that might work for you. However, the traditional and more useful one is the square root approach. There are other ways, but that is by far the simplest, in my opinion.
Edited by rpgfan, 28 January 2008 - 20:14.