Last active
December 15, 2015 18:49
-
-
Save whyrusleeping/5306375 to your computer and use it in GitHub Desktop.
Function to make a list of primes.
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> | |
#include <stdlib.h> | |
#include <math.h> | |
inline | |
int isPrime(int check, int *primes) { | |
for (; (*primes * *primes) < check; primes++) | |
if (check % *primes == 0) return 0; | |
return 1; | |
} | |
int *MakePrimes(int num) { | |
int *primes = malloc(4 * num); | |
int count = 1; | |
int curNum = 3; //Start searching at 3 | |
primes[0] = 2; //Set our first prime to 2 | |
for (;count < num; curNum++) { | |
if (isPrime(curNum, primes)) { | |
primes[count++] = curNum; | |
} | |
} | |
return primes; | |
} | |
int main() { | |
int *p = MakePrimes(40000); | |
int i = 0; | |
for (; i < 40; i++) { | |
printf("%d\n",p[i]); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment