Skip to content

@b00stfr3ak /c_prime.c
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/*
* =====================================================================================
*
* 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.