TEXT   285   0
   135 1.21 KB    48

primes.c

By Guest
Created: 2021-10-09 11:53:24
Expiry: Never

  1. 1.
    #include <stdio.h>
  2. 2.
    #include <math.h>
  3. 3.
    #include <stdlib.h>
  4. 4.
    const int max = 500000000; //checking all numbers between 2 and max*2 +1
  5. 5.
    int main() {
  6. 6.
     
  7. 7.
    char *numbers;
  8. 8.
    numbers = (char *) malloc(sizeof(char)*max);
  9. 9.
     
  10. 10.
    //int primes = 1; //2 is not in the inital list, so start off with 2 as a prime
  11. 11.
    unsigned int currentNumber = 3;
  12. 12.
    unsigned int arrayPosition = 0;
  13. 13.
    unsigned int maxNum = (int) sqrtf((float)max*2)/2;
  14. 14.
    unsigned int crossOff, x;
  15. 15.
     
  16. 16.
    for(x = 0; x < max; x++) {
  17. 17.
    numbers[x] = 1; //set everything to true
  18. 18.
    }
  19. 19.
     
  20. 20.
    while(arrayPosition <= maxNum){
  21. 21.
     
  22. 22.
    if(numbers[arrayPosition]){
  23. 23.
    crossOff = arrayPosition + currentNumber;
  24. 24.
    //printf("crossing out multiples of %d\n", currentNumber);
  25. 25.
    while(crossOff < max){
  26. 26.
    numbers[crossOff] = 0;
  27. 27.
    crossOff = crossOff + currentNumber;
  28. 28.
    }
  29. 29.
    //primes++;
  30. 30.
    }
  31. 31.
    arrayPosition++;
  32. 32.
    currentNumber = arrayPosition*2 + 3;
  33. 33.
    }
  34. 34.
     
  35. 35.
    int primes = 1; //start counting with 2
  36. 36.
    //printf("2 is a prime\n");
  37. 37.
    for(arrayPosition = 0; arrayPosition < max; arrayPosition++){
  38. 38.
    if(numbers[arrayPosition]){
  39. 39.
    //currentNumber = arrayPosition*2 + 3;
  40. 40.
    //printf("%d is a prime\n", currentNumber);
  41. 41.
    primes++;
  42. 42.
    }
  43. 43.
    }
  44. 44.
     
  45. 45.
    printf("Primes found: %d\n", primes);
  46. 46.
     
  47. 47.
    return 0;
  48. 48.
    }

Yandere Thread - Yandere Applejack (completed)

by Guest

Bonding with Nature

by Guest

The Long and Short of It (RGRE)

by Guest

incest relationships piece of the whole pie (lewd) by Frostybox[...]

by Guest

incest thread piece of the (non-canon) pie, limestone's pie by[...]

by Guest