## Project Euler in LaTeX: Problem 2

I probably won't do any more of these, and I hadn't really planned on even doing this one, but I've been inspired. Apparently some Russians found my first post about doing Project Euler problems in LaTeX. According to google translate, my post was described as being part of "the horrors of our Internet" that our comrade stumbled on inadvertently through searching for something completely unrelated. He finishes his post (according to google translate) by saying "see what perverts are in the world."

Honestly, I think this is hilarious, and in the hopes of conning yet more unsuspecting Russian youths into my web of perversion, I present to you the solution, in LaTeX, to the second Project Euler problem.

The second problem asks us to sum up all of the even-valued Fibonacci Numbers. This is surprisingly not that hard to do in LaTeX. The one catch is that the forloop macro doesn't have a while loop. Rigging up a while loop from a for loop is easy enough. The whole thing is fairly straight forward.

Everything except displaying it, that is! The syntax highlighter I use for wordpress has a bug that converts < into &lt; exclusively with LaTeX code, and there is apparently nothing I can do about it (I can't even find where to submit a bug).  So replace the &lt; on line 29 with < to get the code to compile.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}

\usepackage{forloop}	% for loops
\usepackage{fp}			% floating point

\begin{document}

\section{Project Euler:  Problem 2}

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
\begin{center}
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \dots
\end{center}
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
\section{Solution}

\newcounter{sum}
\newcounter{current}
\newcounter{prev}
\newcounter{prevprev}
\newcounter{n}

\FPset{\sum}{0}
\FPset{\prev}{1}
\FPset{\prevprev}{1}
\FPset{\current}{2}

\forloop{n}{1}{\value{n} < 2}{  % a makeshift while loop
\FPdiv{\test}{\current}{2}

\FPset{\prevprev}{\prev}
\FPset{\prev}{\current}

\FPiflt{\current}{4000000} \setcounter{n}{0} \fi
}

\FPround{\sum}{\sum}{0}

The sum of all even valued Fibonacci Numbers below 4,000,000 is \FPprint{\sum}.

\end{document}

And here is the compiled pdf.

One final note, our Russian friend also said that the code from the first post threw a bunch of errors. It shouldn't. I think there was a bad box in that one, but there definitely shouldn't be errors. There might be issues with using the CTAN macros in Miktex (I use TeX Live with EVERYTHING installed).