<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Eli Bendersky's website - Project Euler</title><link href="https://eli.thegreenplace.net/" rel="alternate"></link><link href="https://eli.thegreenplace.net/feeds/project-euler.atom.xml" rel="self"></link><id>https://eli.thegreenplace.net/</id><updated>2024-05-04T19:46:23-07:00</updated><entry><title>Euler 68 - solving problems the hard way, but having fun along the way</title><link href="https://eli.thegreenplace.net/2009/07/28/euler-68-solving-problems-the-hard-way-but-having-fun-along-the-way" rel="alternate"></link><published>2009-07-28T14:58:35-07:00</published><updated>2024-05-04T19:46:23-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2009-07-28:/2009/07/28/euler-68-solving-problems-the-hard-way-but-having-fun-along-the-way</id><summary type="html">
      &lt;p&gt;
Project Euler &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=68"&gt;problem 68&lt;/a&gt;
is your typical puzzle - you have to fill in some numbers into a pattern
according to certain rules (constraints). Kind-of like Sudoku, just simpler.
      &lt;/p&gt;&lt;p&gt;

The approaches people have used to solve it are quite varied:
      &lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;By hand, using pencil and paper. It isn't a very hard …&lt;/li&gt;&lt;/ul&gt;</summary><content type="html">
      &lt;p&gt;
Project Euler &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=68"&gt;problem 68&lt;/a&gt;
is your typical puzzle - you have to fill in some numbers into a pattern
according to certain rules (constraints). Kind-of like Sudoku, just simpler.
      &lt;/p&gt;&lt;p&gt;

The approaches people have used to solve it are quite varied:
      &lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;By hand, using pencil and paper. It isn't a very hard puzzle, after all.&lt;/li&gt;
	&lt;li&gt;Brute forcing the 10! permutations of 10 numbers, checking which permutation satisfies all the constraints and has the largest 16-digit string representation.&lt;/li&gt;
	&lt;li&gt;Smarter brute forcing with some pruning of bad paths - this comes closer to AI approaches.&lt;/li&gt;
	&lt;li&gt;Some people recognized that this can be easily solved by a constraint-satisfaction engine, found such an engine and used it to solve the problem (there's still a challenge here, and that's representing the problem in the language of the constraint satisfier).&lt;/li&gt;
	&lt;li&gt;The most brilliant solution I saw was using SQL! The constraints were coded as a SQL &lt;code&gt;select&lt;/code&gt; statement - not the most efficient solution, but an original one!&lt;/li&gt;
&lt;/ul&gt;


      &lt;p&gt;
Now, as I tend to do in many cases, I took the long and hard way here. I coded
my own constraint satisfaction engine in Python - complete with constraint
propagation, search pruning and some generic heuristics. For sample problems I
represented map coloring, Sudoku, magic squares and N-queens problems. The
solver works quite fine for all these problems, and when I represented the 5-gon
problem for it, it was easy to find the solution quickly.
      &lt;/p&gt;&lt;p&gt;

It was real fun coding the CSP solver (kinda like the time when I coded a simple
&lt;a href="https://eli.thegreenplace.net/2007/04/08/sudoku-as-a-sat-problem/"&gt;SAT solver to solve Sudokus&lt;/a&gt;) - it was an educational experience. True, solving problem 68 took 6-7 hours instead of 20 minutes this way, but it was definitely worth it!
      &lt;/p&gt;&lt;p&gt;

The code for the solver is available &lt;a href="https://github.com/eliben/code-for-blog/tree/main/2009/csp_for_euler68"&gt;here&lt;/a&gt;. Drop me an email if you have any questions about it.
      &lt;/p&gt;
    </content><category term="misc"></category><category term="Project Euler"></category></entry><entry><title>Solved 100th problem on Project Euler!</title><link href="https://eli.thegreenplace.net/2009/06/27/solved-100th-problem-on-project-euler" rel="alternate"></link><published>2009-06-27T07:15:51-07:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2009-06-27:/2009/06/27/solved-100th-problem-on-project-euler</id><summary type="html">
        I've just solved my 100th problem on &lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt; and got this nice message:

&lt;blockquote&gt;Excellent work, eliben! By solving your 100th problem you have earned a place among the top 2.53% of members and have advanced to level 3.
&lt;/blockquote&gt;

So I'm &lt;a href="http://projecteuler.net/index.php?section=profile&amp;amp;profile=eliben"&gt;level 3&lt;/a&gt; now!

&lt;img src="https://eli.thegreenplace.net/images/2009/06/octahedron.gif" title="octahedron" width="100" height="104" class="alignnone size-full wp-image-1751" /&gt;

P.S.: Problem 125 is …</summary><content type="html">
        I've just solved my 100th problem on &lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt; and got this nice message:

&lt;blockquote&gt;Excellent work, eliben! By solving your 100th problem you have earned a place among the top 2.53% of members and have advanced to level 3.
&lt;/blockquote&gt;

So I'm &lt;a href="http://projecteuler.net/index.php?section=profile&amp;amp;profile=eliben"&gt;level 3&lt;/a&gt; now!

&lt;img src="https://eli.thegreenplace.net/images/2009/06/octahedron.gif" title="octahedron" width="100" height="104" class="alignnone size-full wp-image-1751" /&gt;

P.S.: Problem 125 is very easy

    </content><category term="misc"></category><category term="Project Euler"></category></entry><entry><title>Project Euler problem 83 - how creeps can help</title><link href="https://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help" rel="alternate"></link><published>2009-06-19T17:10:47-07:00</published><updated>2023-02-04T13:41:52-08:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2009-06-19:/2009/06/19/project-euler-problem-83-how-creeps-can-help</id><summary type="html">
&lt;p&gt;
        Problems 18, 67, 81, 82 and 83 in Project Euler are quite similar, but increase in level of difficulty.
&lt;/p&gt;&lt;p&gt;

For all of these except 83 I've used a specialized dynamic-programming solution that worked quite well. Being recursive it was a tiny bit slower than the iterative approaches others have used …&lt;/p&gt;</summary><content type="html">
&lt;p&gt;
        Problems 18, 67, 81, 82 and 83 in Project Euler are quite similar, but increase in level of difficulty.
&lt;/p&gt;&lt;p&gt;

For all of these except 83 I've used a specialized dynamic-programming solution that worked quite well. Being recursive it was a tiny bit slower than the iterative approaches others have used, but I believe recursive solutions are more elegant.
&lt;/p&gt;&lt;p&gt;

Anyway, project 83 proved too hard for my approach, because it introduces real cycles into the paths. So, this time I had to arm myself with a real path-finding algorithm. Luckily, I've coded a pretty generic implementation of the A* algorithm quite recently for my &lt;a href="https://eli.thegreenplace.net/2009/01/09/writing-a-game-in-python-with-pygame-part-iii/"&gt;Creeps PyGame demo&lt;/a&gt;. 
&lt;/p&gt;&lt;p&gt;

A*, however, requires a heuristic to do its magic, and for problem 83 there's no real heuristic. However, since a constant heuristic is acceptable and effectively "dumbs down" A* to the original Dijkstra algorithm (the heuristic of A* must be non-decreasing, which a constant satisfies), I was able to make use of the code from the Creeps game to find a solution in 0.3 seconds, with only 30 lines of code.
&lt;/p&gt;

    </content><category term="misc"></category><category term="Project Euler"></category></entry><entry><title>Project Euler problem 66 and continued fractions</title><link href="https://eli.thegreenplace.net/2009/06/19/project-euler-problem-66-and-continued-fractions" rel="alternate"></link><published>2009-06-19T14:49:07-07:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2009-06-19:/2009/06/19/project-euler-problem-66-and-continued-fractions</id><summary type="html">
        &lt;p&gt;&lt;a class="reference external" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=66"&gt;Problem 66&lt;/a&gt; is one of those problems that make Project Euler lots of fun. It doesn't have a brute-force solution, and to solve it one actually has to implement a non-trivial mathematical algorithm and get exposed to several interesting techniques.&lt;/p&gt;
&lt;p&gt;I will not post the solution or the full code …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;&lt;a class="reference external" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=66"&gt;Problem 66&lt;/a&gt; is one of those problems that make Project Euler lots of fun. It doesn't have a brute-force solution, and to solve it one actually has to implement a non-trivial mathematical algorithm and get exposed to several interesting techniques.&lt;/p&gt;
&lt;p&gt;I will not post the solution or the full code for the problem here, just a couple of hints.&lt;/p&gt;
&lt;p&gt;After a very short bout of Googling, you'll discover that the Diophantine equation:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://eli.thegreenplace.net/images/math/cdc11e760e8b319f652e19c6daf547cbe9d0b0f9.gif" /&gt;&lt;/p&gt;
&lt;p&gt;Is quite famous and is called &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Pell%27s_equation"&gt;Pell's equation&lt;/a&gt;. From here, further web searches and Wikipedia-reading will bring you to at least two methods for finding the &lt;em&gt;fundamental solution&lt;/em&gt;, which is the pair of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;x&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;y&lt;/span&gt;&lt;/tt&gt; with minimal &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;x&lt;/span&gt;&lt;/tt&gt; solving it.&lt;/p&gt;
&lt;p&gt;One of the methods involves computing the continued-fraction representation of the square root of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;D&lt;/span&gt;&lt;/tt&gt;. &lt;a class="reference external" href="http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html"&gt;This page&lt;/a&gt; is a must read on this topic, and will help you with other Euler problems as well.&lt;/p&gt;
&lt;p&gt;I want to post here a code snippet that implements the continued-fraction computation described in that link. Its steps follow the &lt;em&gt;Algebraic algoritm&lt;/em&gt; given there:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #00007f; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #00007f"&gt;CF_of_sqrt&lt;/span&gt;(n):
    &lt;span style="color: #7f007f"&gt;&amp;quot;&amp;quot;&amp;quot; Compute the continued fraction representation of the&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        square root of N.&lt;/span&gt;

&lt;span style="color: #7f007f"&gt;        The first element in the returned array is the whole&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        part of the fraction. The others are the denominators&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        up to (and not including) the point where it starts&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        repeating.&lt;/span&gt;

&lt;span style="color: #7f007f"&gt;        Uses the algorithm explained here:&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        In the section named: &amp;quot;Methods of finding continued&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;        fractions for square roots&amp;quot;&lt;/span&gt;
&lt;span style="color: #7f007f"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span style="color: #00007f; font-weight: bold"&gt;if&lt;/span&gt; is_square(n):
        &lt;span style="color: #00007f; font-weight: bold"&gt;return&lt;/span&gt; [&lt;span style="color: #00007f"&gt;int&lt;/span&gt;(math.sqrt(n))]

    ans = []

    step1_num = &lt;span style="color: #007f7f"&gt;0&lt;/span&gt;
    step1_denom = &lt;span style="color: #007f7f"&gt;1&lt;/span&gt;

    &lt;span style="color: #00007f; font-weight: bold"&gt;while&lt;/span&gt; &lt;span style="color: #00007f"&gt;True&lt;/span&gt;:
        nextn = &lt;span style="color: #00007f"&gt;int&lt;/span&gt;((math.floor(math.sqrt(n)) + step1_num) / step1_denom)
        ans.append(&lt;span style="color: #00007f"&gt;int&lt;/span&gt;(nextn))

        step2_num = step1_denom
        step2_denom = step1_num - step1_denom * nextn

        step3_denom = (n - step2_denom ** &lt;span style="color: #007f7f"&gt;2&lt;/span&gt;) / step2_num
        step3_num = -step2_denom

        &lt;span style="color: #00007f; font-weight: bold"&gt;if&lt;/span&gt; step3_denom == &lt;span style="color: #007f7f"&gt;1&lt;/span&gt;:
            ans.append(ans[&lt;span style="color: #007f7f"&gt;0&lt;/span&gt;] * &lt;span style="color: #007f7f"&gt;2&lt;/span&gt;)
            &lt;span style="color: #00007f; font-weight: bold"&gt;break&lt;/span&gt;

        step1_num, step1_denom = step3_num, step3_denom

    &lt;span style="color: #00007f; font-weight: bold"&gt;return&lt;/span&gt; ans
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As I said, this still isn't enough to solve the problem, but with this code in hand, the solution isn't too far. Read some more about Pell's equation and you'll discover how to use this code to reach a solution.&lt;/p&gt;
&lt;p&gt;It took my program ~30 milliseconds to find an answer to the problem, by the way. It took less than a second to solve a 10-times larger problem (for D &amp;lt;= 10000), so I believe it to be a pretty good implementation.&lt;/p&gt;

    </content><category term="misc"></category><category term="Math"></category><category term="Project Euler"></category><category term="Python"></category></entry><entry><title>Posting Project Euler solutions in my blog</title><link href="https://eli.thegreenplace.net/2009/03/02/posting-project-euler-solutions-in-my-blog" rel="alternate"></link><published>2009-03-02T18:49:09-08:00</published><updated>2023-02-04T13:41:52-08:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2009-03-02:/2009/03/02/posting-project-euler-solutions-in-my-blog</id><summary type="html">
      &lt;p&gt;
Some may find it inappropriate that I post solutions to Project Euler in this
blog. I don't share this sentiment, however. Here's an excerpt from the PE FAQ:
      &lt;/p&gt;&lt;p&gt;

&lt;blockquote&gt;
I solved it by using a search engine, does that matter?

That depends on your motivation for solving the problems. It probably …&lt;/blockquote&gt;&lt;/p&gt;</summary><content type="html">
      &lt;p&gt;
Some may find it inappropriate that I post solutions to Project Euler in this
blog. I don't share this sentiment, however. Here's an excerpt from the PE FAQ:
      &lt;/p&gt;&lt;p&gt;

&lt;blockquote&gt;
I solved it by using a search engine, does that matter?

That depends on your motivation for solving the problems. It probably means that you've missed out on some beautiful and hidden mathematics.
&lt;/blockquote&gt;
      &lt;/p&gt;&lt;p&gt;

If anyone races through PE using solutions of others, what has he gained? Will
others feel they've been robbed? Isn't this, at the core, just an issue of
personal integrity?
      &lt;/p&gt;&lt;p&gt;

Anyways, if anyone's determined to find solutions to PE online, it's easy. This
&lt;a href="http://www.haskell.org/haskellwiki/Project_euler"&gt;Haskell Wiki&lt;/a&gt;, for
instance, holds code for the first 200 solutions. And other resources can be
easily found by Googling.
      &lt;/p&gt;&lt;p&gt;

I'm posting hints for some selected problems because I feel like sharing these
insights with the readers of my blog, and this is also a good way to compare
solutions with friends. It's the same motivation I've used for posting the
solutions to almost all &lt;a href="https://eli.thegreenplace.net/tag/sicp"&gt;SICP
exercises online&lt;/a&gt;. For some reason no one objected to that, although solved
SICP exercises can lead to students cheating in homework, while Project Euler is
just a game.
      &lt;/p&gt;&lt;p&gt;

So take it easy, and if you don't want to spoil the solving experience for
yourself, just don't look at these posts! I've conveniently hidden all the real
hints behind a &lt;em&gt;Read More&lt;/em&gt; link, so it won't be accidentally caught in
your eyesight.
      &lt;/p&gt;



    </content><category term="misc"></category><category term="Project Euler"></category></entry></feed>