Created
February 12, 2021 15:34
-
-
Save AggamR/7365f4bcfd5fb0d73887cd563afa28d6 to your computer and use it in GitHub Desktop.
Finding prime numbers with C
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> | |
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