Last active
December 14, 2015 08:41
-
-
Save stmtk1/fce3d37a6b2b9d8d4700 to your computer and use it in GitHub Desktop.
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
/* This program is whether a number is prime number. | |
* | |
* usage | |
* - The number of argument of main function is 1. | |
* - The argument is The number you want to check. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv){ | |
int num=atoi(argv[1]), i, j, index = 2, primes[1000], flag; | |
if(argc != 2){printf("argument is invalid"); exit(1);} | |
primes[0] = 2; | |
primes[1] = 3; | |
for(i = 5; i < sqrt(num); i++){ | |
flag = 1; | |
for(j = 0;j < index;j++){ | |
if((i % primes[j]) == 0){ | |
flag = 0; | |
} | |
} | |
if(flag == 1){ | |
primes[index] = i; | |
index++; | |
} | |
} | |
/* for(i = 0;i < index; i++){ | |
printf("%d\n",primes[i]); | |
} */ | |
flag = 1; | |
for(i = 0;i < index;i++){ | |
if(num%primes[i] == 0){ | |
flag = 0; | |
} | |
} | |
if(num == 2 || num == 3){ | |
flag = 1; | |
} | |
if(flag == 1){ | |
printf("a prime number"); | |
} | |
if(flag == 0){ | |
printf("not a prime number"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
666