Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Last active December 15, 2015 18:49
Show Gist options
  • Save whyrusleeping/5306375 to your computer and use it in GitHub Desktop.
Save whyrusleeping/5306375 to your computer and use it in GitHub Desktop.
Function to make a list of primes.
#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