[ create a new paste ] login | about

Link: http://codepad.org/G4MTqxOS    [ raw code | output | fork ]

C, pasted on Apr 24:
#include<stdio.h>
#include <stdlib.h>
#define MAX 100

int Sieb_des_Eratosthenes()
{
    long long unsigned int i, j, x;
    x = 0;
    char *array;
    array = calloc((MAX + 1), sizeof(char));
    if (array==NULL) {
       printf("Error allocating memory!\n");
       return -1; //return with failure
    }
    
    for(i = 2; i <= sqrt(MAX); i++)
    {
       if(array[i] == 0)     
       {
           //array[i] = 1;
           x++;
           printf("prime %d ", i);
           for(j = i * i; j <= MAX; j += i)
           {
                 array[j] = 1;
           }      
       }
    }
    
    printf("Found: %d\n", x);
    
    free(array);
    array = NULL;
    return 0;
}

main()
{
   return Sieb_des_Eratosthenes();
}


Output:
1
prime 2 prime 3 prime 5 prime 7 Found: 4


Create a new paste based on this one


Comments: