-
-
Save edymanoloiu/2909477 to your computer and use it in GitHub Desktop.
find prime numbers in a vector
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
#include<stdio.h> | |
#include<stdlib.h> | |
#include<math.h> | |
int prim (int n) | |
{ | |
int i,aux=sqrt(n); | |
for(i=2; i<=aux; i++) | |
if(n%i==0) | |
return 0; | |
return 1; | |
} | |
int main(){ | |
int nr,i,*x; | |
printf("nr="); | |
scanf("%d",&nr); | |
x=(int *)malloc(nr*sizeof(int)); | |
for(i=1; i<=nr; i++) | |
{ | |
printf("x[%d]=",i); | |
scanf("%d",&x[i]); | |
} | |
for(i=1; i<=nr; i++) | |
if(prim(x[i])) | |
printf("%d este prim\n",x[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@FULLVLAD Agree, but using sqrt(n) that way is bad too! It will cause the method to be called on every check, causing overhead...it's better to store the sqrt in another variable and check against that!