-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler71.cpp
47 lines (38 loc) · 1.02 KB
/
euler71.cpp
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
//DESCRIPTION: Since the sequence is strictly increasing, the fraction
// directly to the left will be closest fraction to 3/7. We can therefore
// simply scan the denominators and determine the numerator that gives the
// closest to 3/7 without exceeding. Then, we simply take the result that
// resulted in the lowest error.
#include <iostream>
#include <time.h>
#include <math.h>
#include <bitset>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
int main()
{
clock_t time;
time = clock();
double mind = 1e300;
ll ans = -1;
for (ll d = 2; d <= 1000000; d++) {
if (d % 7 == 0) {
continue; // these won't be proper fractions
}
double exact = d * 3.0 / 7.0;
ll numerator = floor(exact);
double dist = (3.0 / 7.0) - ((double)numerator / d);
if (dist < mind) {
mind = dist;
ans = numerator;
}
}
cout << ans << endl;
time = clock() - time;
cout << "\nTime elapsed: " << ((double)time) / CLOCKS_PER_SEC << " seconds.";
cin.get();
return 0;
}