# Project Euler - Question 2

Hey all,
I have just solved the second question in Project Euler…

def project1():
a,b = 1,2
sum = 0

while b < 4000000:
if (b%2 == 0):
sum+=b
a,b = b,a+b

print("Sum: " + str(sum))

project1()


And to be completely honest I don’t know how my own code works

I don’t understand why in my code it only worked if I put < 4000000
shouldn’t it work with < 1000000 ???

What does “work” mean here?

The program should run but will not be summing every Fibonacci number whose values do not exceed four million.

I believe you mean that the program does not print the correct number to solve the problem.
The while statement has the ‘b < 4000000’ which prevents the Fibonacci sequence from exceeding four million. If you put 1000000 then the Fibonaci sequence does not exceed one million, which will not be enough values to produce the correct sum.

1 Like

Not your question, but note that the even ones are precisely the ones in positions multiples of 3 (indexing as F_0=0, F_1=1, F_2=1, F_3=2, ...).

We also have the sum

\sum_{k=0}^{n}F_{3n}=\frac{F_{3n+2}-1}{2}

So, you could not be computing sum += b every time. All you need is that when you get to the first F_{3n+3} \geq 4000000, you return the previous Fibonacci F_{3n+2} minus 1 and divided by 2.

Franklin,

There are different approaches to a problem like this and yours is a bit more advanced than a straightforward one. What confused me a bit was not your thoughts but the way they displayed in my mailer. So I looked online and your equations became nicely readable as compared to the TeX equations I am shown in raw form such as:

\sum_{k=0}^{n}F_{3n}=\frac{F_{3n+2}-1}{2}


What I want to know is how you entered the equations so it does show on-line.

I have tried various ways and what seems to work within Discord is using single dollar signs as brackets.

\sum_{k=0}^{n}F_{3n}=\frac{F_{3n+2}-1}{2}

But if it does not get sent to the email version, it is less useful and I am wondering what I might tune, such as maybe what email format I receive.

Yes, enclosing in dollar signs. However, the transformation from TeX to HTML, MathML, SVG, or other ways to display the equations, happens in the browser. The webpage tells the browser to load and run scripts from MathJax.

<script src="https://sea2.discourse-cdn.com/business6/plugins/discourse-math/mathjax/MathJax.2.7.5.js"></script>


The software that you use to read your email might not be able to do the same as your browser.