You are viewing a single comment's thread. Return to all comments →
Sieve to find the primes upto 2000. Then use iterate from -n -> n for a and b and find the largest consecutive primes for a particular a and b.
#include <bits/stdc++.h> using namespace std; vector<int> prime; vector<bool> mark2(2001, true); void sieve() { int limit = floor(sqrt(2000)) + 1; vector<bool> mark1(limit+1, true); for (int i=2; i<=limit; i++) { if (mark1[i]) { prime.push_back(i); for (int j=2*i; j<=limit; j+=i) { mark1[j] = false; } } } mark2[0] = false, mark2[1] = false; for (int i=0; prime[i]; i++) for (int j=2*prime[i]; j<=2000; j+=prime[i]) mark2[j] = false; } int main() { sieve(); int n, max_c = 0, val_a, val_b; cin >> n; for (int b=-n; b<n; b++) { for (int a=-n; a<n; a++) { int c = 0; while (c*c + a*c + b > 0 && mark2[c*c + a*c + b]) c += 1; if (max_c < c) { max_c = c; val_a = a; val_b = b; } } } cout << val_a << " " << val_b << "\n"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #27: Quadratic primes
You are viewing a single comment's thread. Return to all comments →
Sieve to find the primes upto 2000. Then use iterate from -n -> n for a and b and find the largest consecutive primes for a particular a and b.