answersLogoWhite

0

...

for(m = 6; m < n; m += 6){
if(isPrime(m - 1) && isPrime(m + 1)){
printf("(%i, %i)\n", m - 1, m + 1);

}

}

...

/*
a quick'n'dirty algorithm for checking if a number is prime or composite. Returns 1 if the number is prime, 0 otherwise. Takes advantages of the following facts:

  1. with the exception of 2 and 3, all prime numbers are 1 away from a multiple of 6
  2. you don't need to check for divisibility by composite numbers, only primes
  3. you only need to check for divisibility up to the square root of the number in question
*/

int isprime(unsigned int val){
int n, returnval = 1;
if(val == 1) return 0;
if(!(val % 2 && val % 3)) return 0;
if(val % 6 != 1 && val % 6 != 5) return 0;

for(n = 6; (n - 1) * (n - 1) <= val; n++){
if(!(val % (n - 1) && val % (n + 1)){
returnval = 0;
break;

}

}
return returnval;

}

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve

Add your answer:

Earn +20 pts
Q: What is an algorithm to find all primes below a given number?
Write your answer...
Submit
Still have questions?
magnify glass
imp