Guest User

Untitled

a guest
Jun 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  5.  
  6. // Find the sum of all the primes below two million.
  7.  
  8. int main() {
  9.     unsigned long long sum = 0;
  10.    
  11.     int i = 2;
  12.    
  13.     while(i < 2000000) {
  14.         if(isPrime(i) == 1) {
  15.             sum += i;
  16.         }
  17.  
  18.         printf("\n%d: %d", i, sum);
  19.        
  20.         if(i == 2) {
  21.             i = 3;
  22.         } else {
  23.             i = i + 2;
  24.         }
  25.     }
  26.  
  27.     printf("\n\n%d", sum);
  28.  
  29.     return 1;
  30. }
  31.  
  32. int isPrime(int number) {
  33.     int i;
  34.     for(i = 2; i < (int)sqrt(number); i++) {
  35.         if(number % i == 0 && i != number) {
  36.             return 0;
  37.         }
  38.     }
  39.  
  40.     return 1;
  41. }
Add Comment
Please, Sign In to add comment