add primary code
This commit is contained in:
BIN
primtallYey/hei1
Executable file
BIN
primtallYey/hei1
Executable file
Binary file not shown.
BIN
primtallYey/hei2
Executable file
BIN
primtallYey/hei2
Executable file
Binary file not shown.
BIN
primtallYey/hei3
Executable file
BIN
primtallYey/hei3
Executable file
Binary file not shown.
35
primtallYey/p.c
Normal file
35
primtallYey/p.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
bool checkPrime(int p, int *pptr){
|
||||
int sqrt_p = sqrt(p);
|
||||
for(int i = 0; i < sqrt_p; i++){
|
||||
if(p % *(pptr+i) == 0){return false;}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(){
|
||||
double max = pow(10,7);
|
||||
int pi_max = (int)(ceil(1.1*(max/log(max))));
|
||||
int primes[pi_max];
|
||||
primes[0] = 2;
|
||||
int *pptr = &primes[0];
|
||||
int count = 1;
|
||||
|
||||
//calculate primes
|
||||
for(int c = 3; c <= max; c+=2){
|
||||
if(checkPrime(c, pptr)){
|
||||
primes[count] = c;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
//print primes
|
||||
printf("The set of all primes below %d is: ", (int)max);
|
||||
for (int i = 0; i < count; i++){
|
||||
printf("%d ", primes[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
BIN
primtallYey/p.o
Normal file
BIN
primtallYey/p.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user