Skip to content

Instantly share code, notes, and snippets.

@stmtk1
Last active December 14, 2015 08:41
Show Gist options
  • Save stmtk1/fce3d37a6b2b9d8d4700 to your computer and use it in GitHub Desktop.
Save stmtk1/fce3d37a6b2b9d8d4700 to your computer and use it in GitHub Desktop.
/* 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;
}
@stmtk1
Copy link
Author

stmtk1 commented Dec 14, 2015

check whether a number is prime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment