-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEuler_Problem-065.b93
43 lines (33 loc) · 1.2 KB
/
Euler_Problem-065.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
v $$$ ####################################################################### // numerator
####################################################################### // denominator
v -1<
>"F">:9+"0"\0p:9+"0"\2p:|
v"c"p040p2"O1"p0"O0" $<
>$1 v@.<v02-"0"p0g03:g2g03-"0"g0:p03:<-1<
>:1-3%:!| >:1+3/2*v +>g*+40g+:55+%"0"+30g2p55+/40p:9-|
>:| >2-#^_1 >20 p "O"v # $
| ># #+ #1 #: #- #12# ^# ># ^# <
$ >\v >\:!|
>0"F">:9+2g"0"-:| >:!|1 +<
^ -1_ ^#1<
[2, 0] frac
[3, 0] (calc) pos
[4, 0] (calc) carry
---------------------------------------
Nice algorithm if you see the pattern in the numerators and denominators.
~~~
denom(n+1) = denom(n) + numer(n) * frac(n)
numer(n+1) = denom(n)
~~~
and the fraction at position n is calculated by ([OEIS-A003417](https://oeis.org/A003417)):
~~~
int GetFrac(int idx)
{
if (idx == 0) return 2;
if ((idx-1) % 3 == 0) return 1;
if ((idx-1) % 3 == 1) return ((idx+1)/3)*2;
if ((idx-1) % 3 == 2) return 1;
return 2;
}
~~~
The rest is just multiplication and long addition (we exceed the 64bit range) a hundred times ...