/// P3(x, a) counts the numbers <= x that have exactly 3 /// prime factors each exceeding the a-th prime. /// Space complexity: O(pi(sqrt(x))). /// int64_t P3(int64_t x, int64_t a, int threads) { print(""); print("=== P3(x, a) ==="); print("Computation of the 3rd partial sieve function"); double time = get_wtime(); vector<int32_t> primes = generate_primes(isqrt(x)); int64_t y = iroot<3>(x); int64_t pi_y = pi_bsearch(primes, y); int64_t sum = 0; threads = ideal_num_threads(threads, pi_y, 100); #pragma omp parallel for num_threads(threads) schedule(dynamic) reduction(+: sum) for (int64_t i = a + 1; i <= pi_y; i++) { int64_t xi = x / primes[i]; int64_t bi = pi_bsearch(primes, isqrt(xi)); for (int64_t j = i; j <= bi; j++) sum += pi_bsearch(primes, xi / primes[j]) - (j - 1); } print("P3", sum, time); return sum; }
int64_t S2_hard(int64_t x, int64_t y, int64_t z, int64_t c, int64_t s2_hard_approx, int threads) { #ifdef HAVE_MPI if (mpi_num_procs() > 1) return S2_hard_mpi(x, y, z, c, s2_hard_approx, threads); #endif print(""); print("=== S2_hard(x, y) ==="); print("Computation of the hard special leaves"); print(x, y, c, threads); double time = get_wtime(); FactorTable<uint16_t> factors(y, threads); int64_t max_prime = z / isqrt(y); vector<int32_t> primes = generate_primes(max_prime); int64_t s2_hard = S2_hard_OpenMP_master((intfast64_t) x, y, z, c, (intfast64_t) s2_hard_approx, primes, factors, threads); print("S2_hard", s2_hard, time); return s2_hard; }
/// Calculate the number of primes below x using the /// Lagarias-Miller-Odlyzko algorithm. /// Run time: O(x^(2/3) / log x) operations, O(x^(1/3) * (log x)^2) space. /// int64_t pi_lmo_parallel3(int64_t x, int threads) { if (x < 2) return 0; double alpha = get_alpha_lmo(x); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t z = x / y; int64_t c = PhiTiny::get_c(y); print(""); print("=== pi_lmo_parallel3(x) ==="); print("pi(x) = S1 + S2 + pi(y) - 1 - P2"); print(x, y, z, c, alpha, threads); int64_t p2 = P2(x, y, threads); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); vector<int32_t> primes = generate_primes(y); int64_t pi_y = primes.size() - 1; int64_t s1 = S1(x, y, c, threads); int64_t s2_approx = S2_approx(x, pi_y, p2, s1); int64_t s2 = S2(x, y, c, s2_approx, primes, lpf, mu, threads); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
size_t euler::problem3() { const auto tester = 600851475143; size_t result; generate_primes( [ tester, &result ]( auto&& prime ) { if( tester % prime == 0 ) result = prime; }, static_cast< size_t >( std::sqrt( tester ) ), std::vector< size_t >{ 2, 3 } ); return result; }
size_t euler::problem35() { const auto limit = 1e6_u; std::set< size_t > primes; generate_primes( [ limit, &primes ]( size_t prime ) { if( prime < limit ) primes.insert( prime ); }, limit, { 2, 3 } ); assert( isCircularPrime( 197, primes ) ); assert( boost::size( boost::make_iterator_range( primes.begin(), boost::lower_bound( primes, 1e2_u ) ) | boost::adaptors::filtered( [ &primes ]( size_t prime ) { return isCircularPrime( prime, primes ); } ) ) + 2 == 13 ); return boost::size( primes | boost::adaptors::filtered( [ &primes ]( size_t prime ) { return isCircularPrime( prime, primes ); } ) ) + 2; }
int main (int argc, char **argv) { std::vector<int> primes = generate_primes (1000); std::vector<int>::iterator p = primes.begin(), e = primes.end(); int total = 0; for (; p != e; ++p) total += *p; std::cout << total << std::endl; return 0; }
void key_generate(long int *d,long int *e,long int *n) { long int PRIME_P,PRIME_Q,count,MODULUS,PHI; long int prime1_index,prime2_index; long int prime_array[1600]; count=generate_primes(prime_array); //There are total 1575(which is value of 'count' variable at this PRIME_Point) //primes between this range, And the program will randomly select any //2-primes from those generated primes. //randomize(); //This intializes seed for random numbers from clock. //That's why 'time.h' is included. :) prime1_index=prime2_index=2; int low=1000; while(prime1_index==prime2_index) { srand(time(NULL)); prime2_index = (rand() % (count-low) ) + low; prime1_index = rand() % low; } PRIME_P=prime_array[prime1_index]; PRIME_Q=prime_array[prime2_index]; printf("PRIME_P:%10ld ( 0x%lx )\n",PRIME_P,PRIME_P); printf("PRIME_Q:%10ld ( 0x%lx )\n",PRIME_Q,PRIME_Q); //Here 2-primes numbers of same bit-length(15 bit) are choosen randomly MODULUS=PRIME_P*PRIME_Q; //This is our MODULUS for 'private' and 'public' key generation PHI=(PRIME_P-1)*(PRIME_Q-1); //This is 'Euler's Totient Function printf("\nMODULUS:%10ld ( 0x%lx )\n",MODULUS,MODULUS); printf("PHI(n) =%10ld ( 0x%lx )\n\nNOTE:phi(n) = phi(p)*phi(q) = (p-1)*(q-1)\n",PHI,PHI); //Now, let public Key Exponent(e)=65537 //Thus, e is coprime to phi(n).(i.e. gcd(e,phi(n)) = 1) //So, the public key is declared as: (e,n) long int PUBLIC_KEY_EXPONENT=65537,PRIVATE_KEY_EXPONENT; //FILE *fp; //fp=fopen("public_key.txt","w+"); PRIVATE_KEY_EXPONENT=mul_inv(PUBLIC_KEY_EXPONENT,PHI); printf("\nThe Public Key(e,n) is: ( %ld ( 0x%lx ), %ld ( 0x%lx ) )\n",PUBLIC_KEY_EXPONENT,PUBLIC_KEY_EXPONENT,MODULUS,MODULUS); printf("\nThe Private Key(d,n) is: ( %ld ( 0x%lx ), %ld ( 0x%lx ) )\n",PRIVATE_KEY_EXPONENT,PRIVATE_KEY_EXPONENT,MODULUS,MODULUS); //Putting Public Key for Public access. //fprintf(fp,"%ld\n%ld",PUBLIC_KEY_EXPONENT,MODULUS); //fclose(fp); *e = PUBLIC_KEY_EXPONENT; *d=PRIVATE_KEY_EXPONENT; //Giving up private key as a *n=MODULUS; //Result of this function. }
size_t euler::problem10() { std::vector< size_t > primes = { 2u, 3u }; typedef boost::coroutines::asymmetric_coroutine< size_t > coro_t; const auto limit = 2 * 1e6_u; coro_t::pull_type source( [ &primes, limit ]( coro_t::push_type& sink ) { generate_primes( sink, limit, primes ); } ); return boost::accumulate( source | boost::adaptors::filtered( std::bind( std::less< size_t >(), std::placeholders::_1, limit ) ), boost::accumulate( primes, 0_u ) ); }
int main(int argc, char** argv) { const int* primes = generate_primes(10000000); const int* last_prime = primes; for (; *last_prime; ++last_prime); --last_prime; for (const int* p = last_prime; p > primes; --p) { if (pandigital((long) *p)) { printf("%d\n", *p); break; } } }
int main() { int tn; int a, b, c; int i, k; generate_primes(); scanf("%d %d %d", &a, &b, &c); k = 0; for (i = a; i <= b; i++){ if (calculate_factors(i, c) == c) k++; } printf("%d\n", k); return 0; }
/* NOTE: Assumes mpz_t's are initted in ku and kp */ int generate_keys(private_key* ku, public_key* kp) { mpz_t phi; mpz_t tmp1; mpz_t tmp2; mpz_init(phi); mpz_init(tmp1); mpz_init(tmp2); /* Insetead of selecting e st. gcd(phi, e) = 1; 1 < e < phi, lets choose e * first then pick p,q st. gcd(e, p-1) = gcd(e, q-1) = 1 */ // We'll set e globally. I've seen suggestions to use primes like 3, 17 or // 65537, as they make coming calculations faster. Lets use 3. mpz_set_ui(ku->e, 3); generate_primes(ku); /* Calculate n = p x q */ mpz_mul(ku->n, ku->p, ku->q); /* Compute phi(n) = (p-1)(q-1) */ mpz_sub_ui(tmp1, ku->p, 1); mpz_sub_ui(tmp2, ku->q, 1); mpz_mul(phi, tmp1, tmp2); mpz_clear(tmp2); /* Calculate d (multiplicative inverse of e mod phi) */ if(mpz_invert(ku->d, ku->e, phi) == 0) { mpz_gcd(tmp1, ku->e, phi); printf("gcd(e, phi) = [%s]\n", mpz_get_str(NULL, 16, tmp1)); printf("Invert failed\n"); mpz_clear(tmp1); mpz_clear(phi); return -1; } mpz_clear(tmp1); mpz_clear(phi); /* Set public key */ mpz_set(kp->e, ku->e); mpz_set(kp->n, ku->n); return 0; }
int64_t S2_easy(int64_t x, int64_t y, int64_t z, int64_t c, int threads) { print(""); print("=== S2_easy(x, y) ==="); print("Computation of the easy special leaves"); print(x, y, c, threads); double time = get_wtime(); vector<int32_t> primes = generate_primes(y); int64_t s2_easy = S2_easy::S2_easy((intfast64_t) x, y, z, c, primes, threads); print("S2_easy", s2_easy, time); return s2_easy; }
int main() { std::vector<size_t> table; table.push_back(2); generate_primes(); for (size_t i = 1; i < bound; ++i) { if (table.back() != 1) { table.push_back(1); } size_t min_idx = table.size() - 1; size_t min_cost = primes[min_idx];; for(size_t idx = 0; idx < table.size() - 1; ++idx) { size_t curr_num = primes[idx]; size_t existing = table[idx]; size_t curr = existing * curr_num; if (min_cost > curr) { min_cost = curr; min_idx = idx; } if (existing == curr_num) { break; } } table[min_idx] *= min_cost; } size_t total = 1; for (size_t curr: table) { total = (total * (curr % modulo)) % modulo; } std::cout << total << std::endl; return 0; }
int main(int argc, char** argv) { const int* primes = generate_primes(LIMIT / 2); int d1[LIMIT + 1]; int d2[LIMIT + 1]; memset(d1, 0, (LIMIT + 1) * sizeof(int)); memset(d2, 0, (LIMIT + 1)* sizeof(int)); identify_divisors(primes, d1, d2); long sum = 0; bool check[LIMIT]; // identifies whether we've seen p*q before; relies on fact that p*q is unique for any primes p,q memset(check, false, LIMIT * sizeof(bool)); for (int n = LIMIT; n > 2; --n) { int p = d1[n]; int q = d2[n]; if (d1[n] > 0 && d2[n] > 0 && !check[p * q]) { check[p * q] = true; sum += n; } } printf("%ld\n", sum); }
/// Calculate the number of primes below x using the /// Lagarias-Miller-Odlyzko algorithm. /// Run time: O(x^(2/3)) operations, O(x^(1/3) * (log x)^2) space. /// int64_t pi_lmo3(int64_t x) { if (x < 2) return 0; double alpha = get_alpha_lmo(x); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t c = PhiTiny::get_c(y); int64_t p2 = P2(x, y, 1); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); vector<int32_t> primes = generate_primes(y); int64_t pi_y = primes.size() - 1; int64_t s1 = S1(x, y, c, 1); int64_t s2 = S2(x, y, c, primes, lpf, mu); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
int64_t S2_easy(int64_t x, int64_t y, int64_t z, int64_t c, int threads) { #ifdef HAVE_MPI if (mpi_num_procs() > 1) return S2_easy_mpi(x, y, z, c, threads); #endif print(""); print("=== S2_easy(x, y) ==="); print("Computation of the easy special leaves"); print(x, y, c, threads); double time = get_wtime(); vector<int32_t> primes = generate_primes(y); int64_t s2_easy = S2_easy_OpenMP((intfast64_t) x, y, z, c, primes, threads); print("S2_easy", s2_easy, time); return s2_easy; }
/// Calculate the number of primes below x using the /// Deleglise-Rivat algorithm. /// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space. /// int64_t pi_deleglise_rivat_parallel1(int64_t x, int threads) { if (x < 2) return 0; double alpha = get_alpha(x, 0.0017154, -0.0508992, 0.483613, 0.0672202); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t z = x / y; int64_t p2 = P2(x, y, threads); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); vector<int32_t> primes = generate_primes(y); int64_t pi_y = pi_bsearch(primes, y); int64_t c = PhiTiny::get_c(y); int64_t s1 = S1(x, y, c, threads); int64_t s2 = S2(x, y, z, c, primes, lpf, mu, threads); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
/* ---------------------------------------------------------------------------- * Funktion: calculate_rsa_keys * ------------------------------------------------------------------------- */ extern void calculate_rsa_keys(void) { int p = 0; /* p und q sind zwei zufällig ausgewählte Primzahlen */ int q = 0; /* im Intervall von 2 bis LIMIT */ int phi_n; /* Wert der Eulerschen phi-Funktion: (p - 1) * (q - 1) */ int e = 0; /* Exponent des öffentlichen Schlüssels */ int d = 0; /* Exponent des privaten Schlüssels */ int n; /* RSA-Modul */ int gcd; /* ggt von e und n */ int d_and_k[2] = {0}; /* Hilfsvektor, um die beiden Werte d und k, die im * erweiterten Euklidschen Algorithmus berechnet werden, * zurückzugeben. */ BOOL found_e; /* TRUE, wenn ein teilerfremdes e zu n gefunden wurde, * FALSE sonst. */ /* Berechnung der Primzahlen von 2 bis PRIME_LIMIT */ generate_primes(PRIME_LIMIT); /* * Wähle zwei zufällige Primzahlen, so dass deren Produkt (das RSA-Modul) * größer als der ASCII-Wert des größten zu verschlüsselnden * Zeichens ist und kleiner als die Zahl (PRIME_LIMIT), die die * RSA-Schlüssel nicht überschreiten dürfen. */ n = 0; while (n <= MAX_CHAR || n >= PRIME_LIMIT) { p = get_random_prime(LIMIT); q = get_random_prime(LIMIT); n = p * q; } #ifdef DEBUG DPRINT(p); DPRINT(q); DPRINT(n); #endif /* Berechne die eulersche Phi-Funktion von p und q */ phi_n = (p - 1) * (q - 1); #ifdef DEBUG DPRINT(phi_n); #endif /* * Wähle eine zu phi_n teilerfremde Zahl e und berechne d, so dass gilt: * d > 1, e != d, e * d + k * phi_n = 1 = ggt(e, phi_n) */ gcd = -1; while (d <= 1 || d == e || gcd != 1) { /* * Wähle eine Primzahl e, so dass e kleiner ist als phi_n und * e kein Teiler von phi_n ist --> e und phi_n sind teilerfremd. */ found_e = FALSE; while (found_e == FALSE) { e = get_random_prime(phi_n); if (e < phi_n && phi_n % e != 0) { found_e = TRUE; } } #ifdef DEBUG DPRINT(e); #endif /* * Bestimme die Zahl d mit dem erweiterten euklidschen Algorithmus, * so dass gilt: e * d + k * phi_n = 1 = ggT(e, phi_n) */ gcd = extended_gcd(e, phi_n, d_and_k); d = d_and_k[0]; } #ifdef DEBUG DPRINT(phi_n); DPRINT(e); DPRINT(n); DPRINT(d); #endif /* Globale Variablen auf die generierten Schlüsselwerte setzen */ rsa_modul = n; public_exponent = e; secret_exponent = d; #ifdef DEBUG printf("Es wurde folgendes Schluesselpaar erzeugt:\n"); printf("oeffentlicher Schluessel: (%4d, %4d)\n", e, n); printf("privater Schluessel : (%4d, %4d)\n", d, n); printf("\n"); #endif }
int main(int argc, char **argv) { int p_count = 0; int check, i; int working = 0; int (*prog[MAX_PROGRAM_COUNT +1])(void); /** * Unfortunatly, in C there is no way around this map. * Don't forget to redefine MAX_PROGRAM_COUNT in euler.h */ while(p_count<=MAX_PROGRAM_COUNT) prog[p_count++]=no_problem; prog[1] = Problem001; prog[2] = Problem002; prog[3] = Problem003; prog[4] = Problem004; prog[5] = Problem005; prog[6] = Problem006; prog[7] = Problem007; prog[8] = Problem008; prog[9] = Problem009; prog[10] = Problem010; prog[11] = Problem011; prog[12] = Problem012; prog[13] = Problem013; prog[14] = Problem014; prog[15] = Problem015; prog[16] = Problem016; prog[17] = Problem017; prog[18] = Problem018; prog[20] = Problem020; prog[21] = Problem021; prog[23] = Problem023; prog[25] = Problem025; prog[34] = no_problem; prog[45] = Problem045; prog[48] = Problem048; prog[53] = Problem053; prog[57] = Problem057; prog[59] = Problem059; printf("Prime generator mode: %d\n", generate_primes(10000000)); if(argc>1) { /** * for each number specified on command line, run that problem * ignore others */ i = 1; while(argc>i) { if(sscanf(argv[i++], "%d", &p_count)) { printf("Problem%3d: ", p_count); if(p_count > MAX_PROGRAM_COUNT || wrap(prog[p_count])) { printf("NOT IMPLEMENTED !!!"); } } printf("\n"); } } else { /** * Select each sub program in turn or die. */ for(p_count=1;p_count <= MAX_PROGRAM_COUNT;p_count++) { printf("Problem%3d: ", p_count); check = wrap(prog[p_count]); switch (check) { case 0: /* Success */ working++; printf("\n"); break; case 1: default: /* Fail */ printf("\b\b\b\b\b\b\b\b\b\b\b\b"); break; case 2: /* Memory/File error */ printf("Memory or File allocation error\n"); break; } } printf("%d Problems solved", working); } printf("\nTotal program time is %.3fs\n", (double) clock()/CLOCKS_PER_SEC ); return 0; }