Prime numbers are also called prime numbers. The so-called prime is the number that cannot be divisible by any integer except 1 and itself, for example, 17 is a prime, because it cannot be divisible by any integer of 2~16.
Idea 1): So to determine whether an integer m is a prime number, just the M is 2 ~ m-1 each integer between the removal, if not divisible, then M is a prime.
Idea 2): Another method of judging can be simplified. M does not have to be removed from each integer between 2 and M-1, just by removing each integer from 2 to one. If m cannot be divisible by 2 to any integer, M must be a prime number. For example, to determine whether 17 is a prime number, just so that 17 is each of the integers between the removal, because it is not divisible, you can determine that 17 is a prime number.
Cause: Because if M can be divisible by any integer between 2 ~ m-1, its two factors must have one less than or equal to, and the other greater than or equal to. For example, 16 can be 2,4,8 divisible, 16=2*8,2 is less than 4, 8 is greater than 4,16=4*4,4=√16, so only to determine whether there is no factor between the few.
Idea 1:
1 intIsPrime (intx)2 {3 intI,a;4 for(i=2; i<x-1; i++)//from 2 to x-1, see if we can divide it.5 {6a=x%i;7 if(a==0)//The divisible description is not a prime number, returns 2,8{return 2; Break;}9 }Ten if(i== (x1))//The entire for loop ends and the loop is not divisible at the end, indicating that the prime number returns 1 One return 1; A}
Idea 2:
1 intIsPrime2 (intx)2 {3 intI,a;4 if(x==2)return 1;5 for(i=int(sqrt (x));i>2; i--)//from 2 to x-1, see if we can divide it.6 {7a=x%i;8 if(a==0)//The divisible description is not a prime number, returns 2,9{return 2; Break;}Ten } One if(i==2)//The entire for loop ends and the loop is not divisible at the end, indicating that the prime number returns 1 A return 1; -}
C language Determines whether a number is a prime