@@ -12,7 +12,7 @@ If we list all the natural numbers below $10$ that are multiples of $3$ or $5$,
|
|||||||
|
|
||||||
Suppose the base numbers were $2$ and $3$ below $10$ so $9$, they would create duplicates, ie $6$ is counted twice. Therefore take the sum of each multiple to the limit, and one time remove the numbers counted twice:
|
Suppose the base numbers were $2$ and $3$ below $10$ so $9$, they would create duplicates, ie $6$ is counted twice. Therefore take the sum of each multiple to the limit, and one time remove the numbers counted twice:
|
||||||
|
|
||||||
#html.frame(
|
#text(fill: white, html.frame(
|
||||||
$
|
$
|
||||||
&2+4+6+8+3+cancel(6)+9 = 32\
|
&2+4+6+8+3+cancel(6)+9 = 32\
|
||||||
&sum_(i=1)^(floor(9/(2)))2i + sum_(i=1)^(floor(9/(3))) 3i - sum_(i=1)^(floor(9/(6))) 6i \
|
&sum_(i=1)^(floor(9/(2)))2i + sum_(i=1)^(floor(9/(3))) 3i - sum_(i=1)^(floor(9/(6))) 6i \
|
||||||
@@ -21,17 +21,17 @@ $
|
|||||||
= 2& (4(5))/2 + 3 (3(4))/2 - 6 (1(2))/2\
|
= 2& (4(5))/2 + 3 (3(4))/2 - 6 (1(2))/2\
|
||||||
= 3&2
|
= 3&2
|
||||||
$
|
$
|
||||||
)
|
))
|
||||||
|
|
||||||
This revealed a general solution, for base numbers $n, m$ below $L$:
|
This revealed a general solution, for base numbers $n, m$ below $L$:
|
||||||
|
|
||||||
#html.frame(
|
#text(fill: white, html.frame(
|
||||||
$
|
$
|
||||||
f(n,m,L) =&n sum_(i=1)^floor((L-1)/n) i + m sum_(i=1)^floor((L-1)/m) i- n m sum_(i=1)^floor((L-1)/(n m))i \
|
f(n,m,L) =&n sum_(i=1)^floor((L-1)/n) i + m sum_(i=1)^floor((L-1)/m) i- n m sum_(i=1)^floor((L-1)/(n m))i \
|
||||||
"Define "&a = floor((L-1)/n), b = floor((L-1)/m), c = floor((L-1)/(n m)) \
|
"Define "&a = floor((L-1)/n), b = floor((L-1)/m), c = floor((L-1)/(n m)) \
|
||||||
=> f(n,m,L) =& (n a(a+1) + m b(b+1) - n m (c)(c+1))/2
|
=> f(n,m,L) =& (n a(a+1) + m b(b+1) - n m (c)(c+1))/2
|
||||||
$
|
$
|
||||||
)
|
))
|
||||||
|
|
||||||
Testing this as code:
|
Testing this as code:
|
||||||
```py
|
```py
|
||||||
@@ -64,8 +64,8 @@ The goal is to not add the number if it was already added.
|
|||||||
|
|
||||||
Mathamatically this can be done piecewise. If a previous element of $A$ divides $A_i k$, skip that number.
|
Mathamatically this can be done piecewise. If a previous element of $A$ divides $A_i k$, skip that number.
|
||||||
|
|
||||||
#html.frame(
|
#text(fill: white, html.frame(
|
||||||
$
|
$
|
||||||
f(A, L) = sum_(i)^abs(A)[sum_(k=1)^floor((L-1)/A_i) cases(0 quad & A_j divides A_i k and j < k, A_i k quad & A_j divides.not A_i k and j < k)]
|
f(A, L) = sum_(i)^abs(A)[sum_(k=1)^floor((L-1)/A_i) cases(0 quad & A_j divides A_i k and j < k, A_i k quad & A_j divides.not A_i k and j < k)]
|
||||||
$
|
$
|
||||||
)
|
))
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 9.4 KiB |
@@ -0,0 +1,79 @@
|
|||||||
|
#let post_slug = "pe-p2"
|
||||||
|
#let post_preview_image = "image.png"
|
||||||
|
#let post_summary = "Solution to Project Euler Problem 2"
|
||||||
|
#let post_date = "2026-07-29"
|
||||||
|
|
||||||
|
#set math.mat(delim: "[")
|
||||||
|
|
||||||
|
= Project Euler Problem 2
|
||||||
|
|
||||||
|
== Statement
|
||||||
|
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
|
||||||
|
|
||||||
|
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||||
|
|
||||||
|
== Exploration
|
||||||
|
|
||||||
|
|
||||||
|
The even terms in the sequence are:
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
$ cal(F) = {0, cancel(1), cancel(1), 2, cancel(3), cancel(5), 8, cancel(13), cancel(21), 34, cancel(55), ...} $
|
||||||
|
))
|
||||||
|
|
||||||
|
This can be computed quickly with code, some Fibonacci sequence equations would be interesting to derive.
|
||||||
|
|
||||||
|
Notice every 3rd number is even, as the previous two numbers are odd, which sums to even. A similar statement can be said about the odd numbers.
|
||||||
|
|
||||||
|
The Fibonacci sequence can be generated with the equation:
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
$ mat(cal(F)_i; cal(F)_(i+1)) = mat(0, 1; 1, 1) mat(cal(F)_(i-1); cal(F)_i) $
|
||||||
|
))
|
||||||
|
Then every third number can be defined with the equation:
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
$
|
||||||
|
&mat(cal(F)_(i+2); cal(F)_(i+3)) = mat(0, 1; 1, 1)^3 mat(cal(F)_(i-1); cal(F)_i)\
|
||||||
|
=>&mat(cal(F)_(i+2); cal(F)_(i+3)) = mat(1, 2; 2, 3) mat(cal(F)_(i-1); cal(F)_i)
|
||||||
|
$
|
||||||
|
))
|
||||||
|
Also something interesting I noticed for #text(fill: white, html.frame($cal(F) = {0, 1, 1, 2, 3, 5, 8, ...} (cal(F)_0 = 0, i>= 1)$)) is:
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
$
|
||||||
|
mat(0, 1; 1, 1)^i = mat(cal(F)_(i-1), cal(F)_(i); cal(F)_(i), cal(F)_(i+1))
|
||||||
|
$
|
||||||
|
))
|
||||||
|
|
||||||
|
Playing with that leads to the similar equation
|
||||||
|
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
[$cal(F)_(i+j) = cal(F)_(i) cal(F)_(j+1) + cal(F)_(i-1) cal(F)_(j)$,\ thus $cal(F)_(i+3) = 3cal(F)_i + 2cal(F)_(i-1)$]
|
||||||
|
))
|
||||||
|
|
||||||
|
An equation to represent this problem is as follows, however this would need a computer to finish in reasonable time.
|
||||||
|
#text(fill: white, html.frame(
|
||||||
|
$
|
||||||
|
sum_(i=1)^(n: cal(F)_(3n)<4 times 10^6) cal(F)_(3i)
|
||||||
|
$
|
||||||
|
))
|
||||||
|
|
||||||
|
== Solution
|
||||||
|
|
||||||
|
Since every third number is even, take each third element of the sequence until four million. Three step jumps can be taken as defined by the matrix above.
|
||||||
|
|
||||||
|
```py
|
||||||
|
>>> class Pair:
|
||||||
|
... def __init__(self, fi_m1, fi):
|
||||||
|
... self.fi_m1 = fi_m1
|
||||||
|
... self.fi = fi
|
||||||
|
>>> def f_3(pair):
|
||||||
|
... return Pair(
|
||||||
|
... pair.fi_m1 + (2 * pair.fi),
|
||||||
|
... (2 * pair.fi_m1) + (3 * pair.fi)
|
||||||
|
... )
|
||||||
|
>>> x = Pair(1, 2)
|
||||||
|
>>> s = 0
|
||||||
|
>>> while x.fi < 4e6:
|
||||||
|
... s += x.fi
|
||||||
|
... x = f_3(x)
|
||||||
|
>>> print(s)
|
||||||
|
4613732
|
||||||
|
```
|
||||||
+1
-1
@@ -145,7 +145,7 @@ pre {
|
|||||||
padding: 2pt;
|
padding: 2pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
code {
|
code, code * {
|
||||||
font-family: "JetBrains Mono", "Fira Code", "Cascadia Code", "Source Code Pro", monospace;
|
font-family: "JetBrains Mono", "Fira Code", "Cascadia Code", "Source Code Pro", monospace;
|
||||||
font-variant-ligatures: normal;
|
font-variant-ligatures: normal;
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
|
|||||||
Reference in New Issue
Block a user