예제 #1
0
void sum_primes(long n) {
    long sum = 0;
    for(long i = 2; i < n; i++) {
        if(is_prime_fast(i)) {
            //printf("%li\n", i);
            sum += i;
        }
    }
    printf("sum of primes below %li: %li\n", n, sum);
}
int main() {
  int len = 20;
  long long tests[] = {
    -1, 0, 1, 2, 3, 4, 5, 1000000LL, 772023803LL, 792904103LL, 813815117LL,
    834753187LL, 855718739LL, 876717799LL, 897746119LL, 2147483647LL,
    5705234089LL, 5914686649LL, 6114145249LL, 6339503641LL, 6548531929LL
  };
  for (int i = 0; i < len; i++) {
    bool p = is_prime(tests[i]);
    assert(p == is_prime_fast(tests[i]));
    assert(p == is_probable_prime(tests[i]));
  }
  return 0;
}
예제 #3
0
파일: helper.c 프로젝트: 0ip/euler
uint64_t phi(uint64_t n, uint64_t *sieve, uint64_t found) {
    // Base case
    if ( n < 2 )
        return 0;

    // Lehmer's conjecture
    if (is_prime_fast(n, sieve, found)) {
        return n - 1;
    }

    // Even number?
    if ((n & 1) == 0) {
        int m = n >> 1;
        return !(m & 1) ? phi(m, sieve, found)<<1 :
               phi(m, sieve, found);
    }