Last active
April 8, 2018 18:33
-
-
Save pathawks/39c61f51e6b1fcd6b148 to your computer and use it in GitHub Desktop.
allPrimes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author: Pat Hawks | |
* Created on: Aug 28, 2014 | |
* Source File: allPrimes.c | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#include <stdbool.h> | |
bool isPrime(int number) { | |
int maxFactor = (int) sqrt(number); | |
for (int possibleFactor = 2; possibleFactor <= maxFactor; ++possibleFactor) { | |
if (number % possibleFactor == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
for (int number=1; number > 0; ++number) { | |
if (isPrime(number)) { | |
printf( "%d\n", number ); | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author: Pat Hawks | |
* Created on: Aug 28, 2014 | |
* Source File: allPrimes.c | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#include <stdbool.h> | |
bool isPrime(unsigned long number) { | |
if (number <= 1) return false; | |
if (number <= 3) return true; | |
if (number % 2 == 0 || number % 3 == 0) return false; | |
long maxFactor = sqrt(number); | |
for (long possibleFactor = 5; possibleFactor <= maxFactor; possibleFactor += 6) { | |
if (number % possibleFactor == 0 || number % (possibleFactor + 2) == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
const unsigned long max = (1l<<34)-1; | |
for (unsigned long number=1; number <= max; ++number) { | |
if (isPrime(number)) { | |
printf("%lu\n", number); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment