Created
May 14, 2021 23:43
-
-
Save Brandon-Rozek/f8da4fc28f055872c1657576a59b2f20 to your computer and use it in GitHub Desktop.
C code to generate prime numbers
This file contains hidden or 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
#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