void sequential() { int i; for (i = 0; i < JOBS; i++) calc_primes(1000); }
int main(int argc, char *argv[]) { uint64_t max = argc > 1 ? atol(argv[1]) : 1000000000ull; char *bitmap = calloc(max/8 + 1, 1); calc_primes(bitmap, max); printf("%ld\n", count_primes(bitmap, max)); free(bitmap); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { uint64_t max = argc > 1 ? atol(argv[1]) : 1000000000ull; char *bitmap = calloc(max/30 + sqrtl(max) + 16*8, 1); uint64_t adj_max; int a_i; /* adjust max to a multiple of 2,3,5 */ for (adj_max = (max - 1) / 30 * 30 + 1, a_i = 0; adj_max + diff[a_i%8] <= max; adj_max += diff[a_i++%8]) ; calc_primes(bitmap, adj_max); printf("%ld\n", count_primes(bitmap, adj_max / 30 * 8 + num2bit(adj_max)) + 3); free(bitmap); return EXIT_SUCCESS; }
/*! * Calculates all the prime factors of the given value. Does this in a * simplistic manner, using trial division on all values below the sqrt(n). * * \param n Value to factorize * * \return List of factors */ static struct factor *calc_prime_factors(long n, long max_factor, size_t *num_factors) { long i; struct factor *ret = NULL; struct factor *tail = NULL; long *primes; size_t num_primes; primes = calc_primes(max_factor, &num_primes); *num_factors = 0; if(max_factor == 0) { return ret; } for(i = 0; i < num_primes; i++) { if(n % primes[i] == 0) { *num_factors = *num_factors + 1; if(!tail) { ret = malloc(sizeof(struct factor)); tail = ret; } else { tail->next = malloc(sizeof(struct factor)); tail = tail->next; } tail->value = primes[i]; tail->next = NULL; } } if(!ret && n <= max_factor) { *num_factors = 1; ret = malloc(sizeof(struct factor)); ret->value = n; ret->next = NULL; } return ret; }
void * consumer(void *data) { struct job *job; int *cnt; int *item; int total_primes = 0; assert(cnt = malloc(sizeof(*cnt))); *cnt = 0; job = (struct job *) data; while (NULL != (item = (int*) ds_queue_pop(job->job_queue))) { (*cnt)++; total_primes += calc_primes(1000); free(item); } printf("Thread %s consumed %d items (%d primes).\n", job->id, *cnt, total_primes); ds_queue_push(job->results_queue, (void*) cnt); return NULL; }
int problem35(int argc, char** argv) { int i; int cnt1 = 1; // Counts for 2 int tmp; int order; const int end = 1e6; // 1e7; calc_primes(end); for (i = 3; i < end; i += 2){ if (!isPrime(i)) continue; tmp = i; order = 0; while (tmp > 0) { if (tmp % 10 % 2 == 0) break; tmp /= 10; ++order; } if (tmp > 0) continue; if (test_rotations(i, order)) { // printf("Rotations are all primes: %d\n", i); cnt1++; } } printf("Circular Primes under %d: %d\n", end, cnt1); return(0); }
int problem27(int argc, char** argv) { int a; int b; int n; int cnt; int max_num = MAX_NUM; int max_prime_cnt_n = 0; int max_prime_cnt_a = 0; int max_prime_cnt_b = 0; if (argc == 2) { max_num = atoi(argv[1]); if (max_num < 2) { printf("Invalid Max: \"%s\" (%d)\n", argv[1], max_num); return(1); } printf("Setting max number: %d\n", max_num); } cnt = calc_primes(max_num); printf("%d primes under %d\n", cnt, max_num); /* Prime numbers are now determined (any 0 bits in the bitmap) */ for (b = 2; b < max_num; ++b) { /* Non primes are set to 1 in the BM */ /* When n = 0 => b must be a prime alreayd */ if (BM_IS_SET(b, bm_primes)) { continue; } /* If a < -b, then when n = 1, the result would be negative => no prime */ /* a +=2 because when n = 1, (1 + a + b) needs to be an odd number */ for (a = -b; a < max_num; a +=2) { for (n = 1; n < 100; ++n) { if (!isPrime(((n*n) + a*n + b))) { break; } } if (n >= 100) { printf("ERROR: n = %d, (a = %d, b = %d): I don't think it should " "ever get this high\n", n, a, b); } if (n > max_prime_cnt_n) { max_prime_cnt_n = n; max_prime_cnt_a = a; max_prime_cnt_b = b; } } } printf("Largest n = %d when a = %d, b = %d\n", max_prime_cnt_n, max_prime_cnt_a, max_prime_cnt_b); printf("Product of coefficients: %d\n", max_prime_cnt_a * max_prime_cnt_b); if (bm_primes) { free(bm_primes); bm_primes = NULL; } return(0); }