Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // C6
- //
- // Created by Betsalel Williamson on 10/1/11.
- // Chapter 6:
- // Research and implement the Sieve of Eratosthenes.
- #include <stdio.h>
- #include <stdlib.h>
- static int* getSieveOfEratosthenes(int arraySize) {
- /* taken from calloc example on http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/ */
- int* pData;
- pData = (int*) calloc (arraySize,sizeof(int));
- if (pData==NULL) exit (1);
- //fill our new array with numbers
- int counter, currentNumber;
- for (counter = 0; counter < arraySize; counter++) {
- pData[counter]=counter;
- }
- printf ("Primes up to %d\n\n", arraySize);
- for (counter = 0; counter < arraySize; counter++) {
- currentNumber = counter;//a way to keep the position in the counting line
- //prime number logic starts with numbers greater than 1
- if (counter > 1) {
- 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
- pData[counter]=0;//sets non prime to zero
- }
- } else {//accounts for the numbers less than 2
- pData[counter]=0;
- }
- //set the counter back to the current number after misusing the counter variable
- counter = currentNumber;
- }
- return pData;
- }
- static int countNonZeros(int *array, int arraySize) {
- int primeCounter = 0, counter;
- for (counter = 0; counter < arraySize; counter++) {
- if (array[counter]>0) {
- primeCounter++;
- }
- }
- return primeCounter;
- }
- static int countArray(int *array, int arraySize) {
- int primeCounter = 0, counter;
- for (counter = 0; counter < arraySize; counter++) {
- // printf("%x\n", counter);
- if (array[counter]>0) {
- primeCounter++;
- }
- }
- return primeCounter;
- }
- //display prime numbers
- static void printArray(int* array, int arraySize, int whenToSlashN) {
- for (int counter = 0; counter < arraySize; counter++) {
- printf ("%3d ",array[counter]);
- if ((counter+1)%whenToSlashN == 0) {//counter + 1 is there so you start counting at 1 not 0.
- printf("\n");
- }
- }
- }
- static int *stripZerosFromArray(int* array, int arraySize) {
- int* newArray;
- int counter = 0, currentNumber = 0;
- newArray = (int*) calloc (arraySize,sizeof(int));
- if (newArray==NULL) exit (1);
- for (counter=0; counter<arraySize; counter++) {
- if (array[counter] > 0) {
- newArray[currentNumber++] = array[counter];
- }
- }
- //free old array after removing the zeros.
- free(array);
- return newArray;
- }
- int main (int argc, const char * argv[])
- {
- int arraySize = 0;
- int* primeNumbers;
- printf ("Enter the limit: ");
- scanf ("%d",&arraySize);
- primeNumbers = getSieveOfEratosthenes(arraySize);
- primeNumbers = stripZerosFromArray(primeNumbers, arraySize);
- arraySize = countArray(primeNumbers, arraySize);
- // printf("%d", arraySize);
- printArray(primeNumbers, countArray(primeNumbers, arraySize), 11);
- printf("\n\nNumber of primes: %d\n", countNonZeros(primeNumbers, arraySize));
- printf("\n\n");
- //system("pause");
- //freeing the array created with getSieveOfEratosthenes
- free(primeNumbers);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement