Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. #include <gmp.h>
  6.  
  7. int gcd(int a, int b) {
  8.     int gcd = 1;
  9.    
  10.     for(int i = 2; i <= ((a < b) ? a : b); i++)
  11.         if(a % i == 0 && b % i == 0)
  12.             gcd = i;
  13.    
  14.     return gcd;
  15. }
  16.  
  17. int main(int argc, char *argv[]) {
  18.     int n = 100;
  19.    
  20.     std::vector<bool> sieve(n - 1, true);
  21.    
  22.     sieve[0] = false;
  23.     sieve[1] = false;
  24.    
  25.     for(unsigned long i = 2; i <= n; i++) {
  26.         if(sieve[i]) {
  27.             unsigned long k = 2;
  28.             for(unsigned long j = i * k; j < n; k++) {
  29.                 sieve[j] = false;
  30.                
  31.                 j = i * k;
  32.             }
  33.            
  34.             if(sieve[i]) {
  35.                 sieve[i] = false;
  36.                
  37.                 int num = 0;
  38.                 if(n % i == 0) {
  39.                     num = (((n / i) - 1) * i);
  40.                 } else {
  41.                     num = ((floor(n / i)) * i);
  42.                 }
  43.            
  44.                 sieve[num] = true;
  45.             }
  46.         }
  47.     }
  48.    
  49.     int sum = 1;
  50.    
  51.     for(unsigned long i = n; i >= 2; i--) {
  52.         if(sieve[i]) {
  53.             bool goodgcd = true;
  54.             for(unsigned long j = i + 1; j <= n; j++) {
  55.                 if(sieve[j]) {
  56.                     int thisgcd = gcd(i, j);
  57.                     if(thisgcd != 1) {
  58.                         sieve[i] = false;
  59.                         sieve[i / thisgcd] = true;
  60.                         goodgcd = false;
  61.                         break;
  62.                     }
  63.                 }
  64.             }
  65.            
  66.             if(goodgcd) {
  67.                 sum += i;
  68.             }
  69.         }
  70.     }
  71.    
  72.     std::cout << sum << std::endl;
  73. }
Add Comment
Please, Sign In to add comment