Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View c_prime.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
* =====================================================================================
*
* Filename: c_prime.c
*
* Description: find prime numbers up to (n)
*
* Version: 1.0
* Created: 02/17/2014 12:00:27 AM
* Revision: none
* Compiler: gcc -std=c99
*
* Author: b00stfr3ak
*
* =====================================================================================
*/
 
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[]){
if (argc != 2) {
printf("Error: Incorrect usage - c_prime <find_primes_to>\n");
return 1;
}
unsigned long amount = atoi(argv[1]);
int *list;
unsigned long i,x,d;
list = malloc(sizeof(int)*(amount));
if (list == NULL) {
printf("Unable to allocate memory\n");
return 1;
}
for (i=2; i <= amount; i++){
list[i] = 1;
}
for(x=2; x<=amount; x++) {
if (list[x]) {
for (d = x; d * x < amount ; d++) {
list[d * x] = 0;
}
}
}
for (i=2; i < amount; i++){
if (list[i]){
printf("%d\n",i);
}
}
free(list);
return 0;
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.