Skip to content

Instantly share code, notes, and snippets.

@AggamR
Created February 12, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AggamR/7365f4bcfd5fb0d73887cd563afa28d6 to your computer and use it in GitHub Desktop.
Save AggamR/7365f4bcfd5fb0d73887cd563afa28d6 to your computer and use it in GitHub Desktop.
Finding prime numbers with C
#include <stdio.h>
int main( int argc, char *argv[]) {
if (argc < 2)
printf("must supply argument - range");
int devisors = 0, num, devisor, largest, range = atoi(argv[1]);
for (num = 7; num < range; num += 2) {
devisors = 2;
if (num % 5 == 0 || num % 3 == 0)
continue;
for (devisor = 2; devisor < num; devisor++) {
if (num % devisor == 0) {
devisors++;
if (devisors > 2)
break;
}
}
printf("num = %d | devisors = %d\n", num, devisors);
if (devisors == 2)
printf("%d is prime\n", num);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment