Skip to content

Instantly share code, notes, and snippets.

@Brandon-Rozek
Created May 14, 2021 23:43
Show Gist options
  • Save Brandon-Rozek/f8da4fc28f055872c1657576a59b2f20 to your computer and use it in GitHub Desktop.
Save Brandon-Rozek/f8da4fc28f055872c1657576a59b2f20 to your computer and use it in GitHub Desktop.
C code to generate prime numbers
#include <stdio.h>
#include <math.h>
int isPrime(unsigned long long n);
int main() {
printf("%d", 2);
unsigned long long i = 3;
while (1) {
if (isPrime(i)) {
printf("%llu\n", i);
}
i+=2;
}
}
int isPrime(unsigned long long n) {
if (n == 2) {
return 1;
}
for (unsigned long long i = 3; i < n; i+=2) {
if (n % i == 0) {
return 0;
}
if (n / i < i) {
return 1;
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment