Quote (Haxs @ Sep 3 2013 12:17am)
what would be the best way to do this?
like a general flowchart
would u use recursive? or no need?
just do like
input number , input number - 1
then divide the input num by -input number -1 and if remainder = 0 , prime = true
then when it hits 2 then set to false?
That is pretty much it. There are small things you can to do to improve on this, but they are minor.
for all integers greater than 1:
either are prime, or composite
composite numbers can always be represented as the product of 2 or more primes
idea 1 (this one is solid and you should consider using it):
Therefore if the number x is composite, it will have a prime factor that is equal to or smaller than sqrt(x) (round up)
So to find out if X is prime you only need to test if it can be factored by any number in the range 2 to sqrt(x)
idea 2 (this one has scaling problems with very large numbers):
Furthermore if the numbers are small enough, you could create/store a list of known primes (equal too or smaller than the square root of the largest value you plan on testing) and reuse them. For example:
Lets say you want to know what numbers are prime in the range 50 to 60. The square root of 60 is about 7.7 so we will use 8. Simply test if 2 - 8 are prime, then test those primes to see if they are factors of 50 to 60.
This post was edited by Azrad on Sep 3 2013 02:14am