Advertisement
Conner_36

SieveOfEratosthenes

Oct 2nd, 2011
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  C6
  4. //
  5. //  Created by Betsalel Williamson on 10/1/11.
  6. //  Chapter 6:
  7. //  Research and implement the Sieve of Eratosthenes.
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. static int* getSieveOfEratosthenes(int arraySize) {
  13.    
  14.    /* taken from calloc example  on http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/ */
  15.    int* pData;
  16.    pData = (int*) calloc (arraySize,sizeof(int));
  17.    if (pData==NULL) exit (1);
  18.    
  19.    //fill our new array with numbers
  20.    int counter, currentNumber;
  21.    
  22.    for (counter = 0; counter < arraySize; counter++) {
  23.       pData[counter]=counter;
  24.    }
  25.    
  26.    printf ("Primes up to %d\n\n", arraySize);
  27.    
  28.    for (counter = 0; counter < arraySize; counter++) {
  29.       currentNumber = counter;//a way to keep the position in the counting line
  30.      
  31.       //prime number logic starts with numbers greater than 1
  32.       if (counter > 1) {
  33.          for (counter*=2; counter < arraySize; counter+=currentNumber) {//we start with the number times two, as long as its less than the full amount, increment it with the current number
  34.            
  35.             pData[counter]=0;//sets non prime to zero
  36.          }
  37.       } else {//accounts for the numbers less than 2
  38.          pData[counter]=0;
  39.       }
  40.      
  41.       //set the counter back to the current number after misusing the counter variable
  42.       counter = currentNumber;
  43.    }
  44.    
  45.    return pData;
  46. }
  47.  
  48. static int countNonZeros(int *array, int arraySize) {
  49.    
  50.    int primeCounter = 0, counter;
  51.    
  52.    for (counter = 0; counter < arraySize; counter++) {
  53.       if (array[counter]>0) {
  54.          primeCounter++;        
  55.       }
  56.    }
  57.    
  58.    return primeCounter;
  59. }
  60.  
  61. static int countArray(int *array, int arraySize) {
  62.    
  63.    int primeCounter = 0, counter;
  64.    
  65.    for (counter = 0; counter < arraySize; counter++) {
  66. //      printf("%x\n", counter);
  67.       if (array[counter]>0) {
  68.          primeCounter++;        
  69.       }
  70.    }
  71.    
  72.    return primeCounter;
  73. }
  74.  
  75. //display prime numbers
  76. static void printArray(int* array, int arraySize, int whenToSlashN) {
  77.    
  78.    for (int counter = 0; counter < arraySize; counter++) {
  79.       printf ("%3d ",array[counter]);
  80.       if ((counter+1)%whenToSlashN == 0) {//counter + 1 is there so you start counting at 1 not 0.
  81.          printf("\n");
  82.       }
  83.    }
  84. }
  85.  
  86. static int *stripZerosFromArray(int* array, int arraySize) {
  87.    
  88.    int* newArray;
  89.    int counter = 0, currentNumber = 0;
  90.    
  91.    newArray = (int*) calloc (arraySize,sizeof(int));
  92.    if (newArray==NULL) exit (1);
  93.    
  94.    for (counter=0; counter<arraySize; counter++) {
  95.       if (array[counter] > 0) {
  96.          newArray[currentNumber++] = array[counter];
  97.       }
  98.    }
  99.    
  100.    //free old array after removing the zeros.
  101.    free(array);
  102.    return newArray;
  103. }
  104.  
  105. int main (int argc, const char * argv[])
  106. {
  107.    int arraySize = 0;
  108.    int* primeNumbers;
  109.    
  110.    printf ("Enter the limit: ");
  111.    scanf ("%d",&arraySize);
  112.    
  113.    primeNumbers = getSieveOfEratosthenes(arraySize);
  114.    
  115.    primeNumbers = stripZerosFromArray(primeNumbers, arraySize);
  116.    arraySize = countArray(primeNumbers, arraySize);
  117.  
  118. //   printf("%d", arraySize);
  119.  
  120.    printArray(primeNumbers, countArray(primeNumbers, arraySize), 11);
  121.    
  122.    printf("\n\nNumber of primes: %d\n", countNonZeros(primeNumbers, arraySize));
  123.    
  124.    printf("\n\n");
  125.      
  126. //system("pause");
  127.    
  128.    //freeing the array created with getSieveOfEratosthenes
  129.    free(primeNumbers);
  130.    
  131.    return EXIT_SUCCESS;
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement