diff --git a/static/posts/pe-p1/post.typ b/static/posts/pe-p1/post.typ index 1100de9..7503213 100644 --- a/static/posts/pe-p1/post.typ +++ b/static/posts/pe-p1/post.typ @@ -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: -#html.frame( +#text(fill: white, html.frame( $ &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 \ @@ -21,17 +21,17 @@ $ = 2& (4(5))/2 + 3 (3(4))/2 - 6 (1(2))/2\ = 3&2 $ -) +)) 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 \ "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 $ -) +)) Testing this as code: ```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. -#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)] $ -) \ No newline at end of file +)) \ No newline at end of file diff --git a/static/posts/pe-p2/image.png b/static/posts/pe-p2/image.png new file mode 100644 index 0000000..f5e23f7 Binary files /dev/null and b/static/posts/pe-p2/image.png differ diff --git a/static/posts/pe-p2/post.typ b/static/posts/pe-p2/post.typ new file mode 100644 index 0000000..6c148fa --- /dev/null +++ b/static/posts/pe-p2/post.typ @@ -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 +``` \ No newline at end of file diff --git a/static/style.css b/static/style.css index b415c2d..d1952a8 100644 --- a/static/style.css +++ b/static/style.css @@ -145,7 +145,7 @@ pre { padding: 2pt; } -code { +code, code * { font-family: "JetBrains Mono", "Fira Code", "Cascadia Code", "Source Code Pro", monospace; font-variant-ligatures: normal; white-space: pre;