-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler27.cpp
More file actions
59 lines (52 loc) · 1.08 KB
/
Copy pathEuler27.cpp
File metadata and controls
59 lines (52 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <unistd.h>
#include <fstream>
using namespace std;
bool isprime(unsigned long int num){
if(num == 2 || num == 3){
return true;
}
if(num < 2){
return false;
}
if(num%2 == 0 || num%3 == 0){
return false;
}
for(unsigned long int i = 5; i <= pow(num, .5); i+=2 ){
if(num%i == 0){
return false;
}
}
return true;
}
int f(int a, int b ){
vector<int> ret;
for(int i = 0;;i++){
int sol = i*i + a*i + b;
if(sol < 2 || !isprime(sol)){ //dont use isprime for negative integers.
return ret.size();
}
ret.push_back(sol);
}
return ret.size();
}
int main(){
int m = 0;
int besta = 0;
int bestb = 0;
for(int a = -999; a < 1000; a++){
for(int b = -1000; b <= 1000; b++){
int s = f(a,b);
if (s > m){
m = s;
besta = a;
bestb = b;
}
}
}
cout<<"best product: "<<besta*bestb<<" with a size of: "<<m<<endl;
cout<<besta<<' '<<bestb<<endl;
}