eyeRmonkey
Jan 26th, 2006, 07:09 PM
I had heard a long time ago that this was the best way to find primes:
1) Fill an array with all every prime you have found so far (usually start with 2 and 3 as 2 elements that are already in the array)
2) If you want to know if X is prime then divide it by every number in the array.
3) If it X mod Array(i) = 0 then it goes in evenly and it can't be prime.
4) If X is prime then add it to the array and continue
I tried this method and found it was very slow. Here is what I did that gave the same result in 1/1000th of the time:
If you want to know if X is prime then MOD it with every number from I to Sqrt(I) and if none of those = 0 then it is prime.
This may seem obvious to some people, but I thought the first method would be quicker for some reason.
1) Fill an array with all every prime you have found so far (usually start with 2 and 3 as 2 elements that are already in the array)
2) If you want to know if X is prime then divide it by every number in the array.
3) If it X mod Array(i) = 0 then it goes in evenly and it can't be prime.
4) If X is prime then add it to the array and continue
I tried this method and found it was very slow. Here is what I did that gave the same result in 1/1000th of the time:
If you want to know if X is prime then MOD it with every number from I to Sqrt(I) and if none of those = 0 then it is prime.
This may seem obvious to some people, but I thought the first method would be quicker for some reason.