-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEuler_Problem-071.b93
44 lines (28 loc) · 1.22 KB
/
Euler_Problem-071.b93
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
v X X C
X X ????
>020p040p121p141p"}}@"**60pv
v0 < >01+*v
>1+ :3*7%!#v_:61p:3*7/:71p7*61g3*-:0`| >81p61g7*91p41g91g*21g81g*`#v_v
|-g06: < <>01-*^v p04g16p02g17p12g19p14g18<
>$20g."/ ",,55+,40g.@ ^ < <
[20] Result::Denominator
[40] Result::Numerator
[21] Distance::Denominator
[41] Distance::Numerator
[60] LIMIT 1000000
[61] c_n | i
[71] c_d
[81] d_n
[91] d_d
---------------------------------------
For every denominator from `1` to `1 000 000` we generate a possible fraction where the numerator is `(d * 3) / 7`.
(Every other fraction is either greater than `3/7` or has a greater difference than the generated).
Now everything thats left is finding the fraction with the smallest difference to `3/7`.
The difference of two fractions is calculated (without floating points) by:
~~~
n1/d1 - n2/d2 == (n1*d2 - n2*d1)/(d1*d2)
~~~
The rest is just iterating through all the possible fractions and in each step remembering the current best.
We don't even need to reduce the result to get a *proper* fraction.
If it could be reduced we would have got the reduced version first.
(Because it has an smaller denominator).