Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.
First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.
Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).
Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.
First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.
Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).
Try this code
for(int pp=2;pp<100;pp+ +) //pp=possible prime
{
for(int pd=2;pd<pp;pd++ ) //pd=possible divisor
{
if(pp%pd==0)
break;
}
if(pp==pd)
System.out.prin tln(pp);
}
u may declare out of the for loops
int i,j,p;
for(i=2;i<=100; i++)
{
p = 0;
for(j=2;j<=i/2;j++)
{
if(i%j == 0)
{
p = 1;
break;
}
}
if(p == 0)
System.out.prin tln(i);
}
This is exact way.
ex:- no:40, in this number divisible only between 1 to 20, in any case, not divisibly by 21 to 39. So, you must to check the number is prime in off of that number only.
Comment