<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>project-euler &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/project-euler/</link>
	<description>Feed of posts on WordPress.com tagged "project-euler"</description>
	<pubDate>Sat, 05 Dec 2009 18:27:34 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Good boy!]]></title>
<link>http://numericalrecipes.wordpress.com/2009/11/30/good-boy/</link>
<pubDate>Mon, 30 Nov 2009 17:12:45 +0000</pubDate>
<dc:creator>Jaime</dc:creator>
<guid>http://numericalrecipes.wordpress.com/2009/11/30/good-boy/</guid>
<description><![CDATA[Just a quick pat on my own shoulder&#8230; Project Euler&#8217;s problem 266, posted this very last ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Just a quick pat on my own shoulder&#8230;</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#38;id=266">Project Euler&#8217;s</a> <a href="http://projecteuler.net/index.php?section=problems&#38;id=266">problem 266</a>, posted this very last weekend, was proposed by yours truly:</p>
<blockquote><p>The divisors of 12 are: 1,2,3,4,6 and 12.<br />
The largest divisor of 12 that does not exceed the square root of 12 is 3.<br />
We shall call the largest divisor of an integer n that does not exceed the square root of n the pseudo square root (PSR) of n.<br />
It can be seen that PSR(3102)=47.</p>
<p>Let p be the product of the primes below 190.<br />
Find PSR(p) mod 10<sup>16</sup>.</p></blockquote>
<p>I had something much more modest in mind, such as &#8220;all primes below 100&#8243;, since the way I figured it out this was to be solved treating it as a variation of the <a href="http://en.wikipedia.org/wiki/Knapsack_problem">knapsack problem</a> and then using <a href="http://en.wikipedia.org/wiki/Dynamic_programming">dynamic programming</a> on it. An approach that will see you die of old age if you try it for the proposed value&#8230;</p>
<p>But I then had the pleasure of witnessing how <a href="http://projecteuler.net/index.php?section=profile&#38;profile=xan">xan</a>, <a href="http://projecteuler.net/index.php?section=profile&#38;profile=hk">hk</a> and <a href="http://projecteuler.net/index.php?section=profile&#38;profile=daniel.is.fischer">daniel.is.fischer</a> started coming up with alternative, much more sophisticated and elegant methods, that pushed the question all the way up to were it is now. It is both a humbling and edifying experience to see these people at work, so if you have an idea for a problem, do <a href="http://projecteuler.net/index.php?section=contact">propose it</a> to them.</p>
<p>I won&#8217;t go into any more details as to how to solve this particular problem: the right place to ask questions is <a href="http://forum.projecteuler.net/viewtopic.php?f=50&#38;t=1749">here</a>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler - IProblemBase]]></title>
<link>http://cooldotnetstuff.wordpress.com/2009/11/28/project-euler-iproblembase/</link>
<pubDate>Sun, 29 Nov 2009 02:49:25 +0000</pubDate>
<dc:creator>rick schott</dc:creator>
<guid>http://cooldotnetstuff.wordpress.com/2009/11/28/project-euler-iproblembase/</guid>
<description><![CDATA[I have noticed people searching for my IProblemBase class. There is really nothing to it to be hones]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I have noticed people searching for my IProblemBase class.  There is really nothing to it to be honest.  I just have all my Problem[n] classes implement it so that my test harness can call them easily.  What I really need to publish is my BigInt class that most of the solutions I do publish use.  Sorry, it needs some polish so its going to be a while for that one.</p>
<p><strong>IProblemBase</strong></p>
<pre class="brush: csharp;">
namespace Euler
{
    public interface IProblemBase
    {
        string GetAnswer();

    }

}
</pre>
<p><strong>Program</strong></p>
<pre class="brush: csharp;">
namespace Euler
{
    class Program
    {
        static void Main(string[] args)
        {
            IProblemBase p = new Problem48();
            string answer = p.GetAnswer();
            //etc.....
        }

    }

}
</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 1 - Project Euler]]></title>
<link>http://frkarl.wordpress.com/2009/11/28/problem-1-project-euler/</link>
<pubDate>Sat, 28 Nov 2009 02:26:12 +0000</pubDate>
<dc:creator>frkarl</dc:creator>
<guid>http://frkarl.wordpress.com/2009/11/28/problem-1-project-euler/</guid>
<description><![CDATA[Decided that i want to take a look at F# and to solve the project euler problems is a fun way to lea]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Decided that i want to take a look at F# and to solve the project euler problems is a fun way to learn. And here is the first of the problems.</p>
<blockquote><p>
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.
</p></blockquote>
<div style="font-family:Courier New;font-size:10pt;color:black;background:white;"><span style="color:#2b91af;">&#160;&#160;&#160;&#160;5</span>&#160;<span style="color:blue;">let</span> problem1 =<br />
<span style="color:#2b91af;">&#160;&#160;&#160;&#160;6</span>&#160;&#160;&#160;&#160; Seq.sum([<span style="color:blue;">for</span> x <span style="color:blue;">in</span> [1 .. 999] <span style="color:blue;">do</span> <span style="color:blue;">if</span> x%3=0 &#124;&#124; x%5=0 <span style="color:blue;">then</span> <span style="color:blue;">yield</span> x])
</div>
<p></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Maximum Product Consecutive Digits]]></title>
<link>http://windupurnomo.wordpress.com/2009/11/23/maximum-product-consecutive-digits/</link>
<pubDate>Mon, 23 Nov 2009 06:34:33 +0000</pubDate>
<dc:creator>windupurnomo</dc:creator>
<guid>http://windupurnomo.wordpress.com/2009/11/23/maximum-product-consecutive-digits/</guid>
<description><![CDATA[Menentukan nilai maksimal dari 5 bilangan terurut dari 1000 digit bilangan berikut: 7316717653133062]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Menentukan nilai maksimal dari 5 bilangan terurut dari 1000 digit bilangan berikut:</p>
<p>73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
85861560789112949495459501737958331952853208805511<br />
12540698747158523863050715693290963295227443043557<br />
66896648950445244523161731856403098711121722383113<br />
62229893423380308135336276614282806444486645238749<br />
30358907296290491560440772390713810515859307960866<br />
70172427121883998797908792274921901699720888093776<br />
65727333001053367881220235421809751254540594752243<br />
52584907711670556013604839586446706324415722155397<br />
53697817977846174064955149290862569321978468622482<br />
83972241375657056057490261407972968652414535100474<br />
82166370484403199890008895243450658541227588666881<br />
16427171479924442928230863465674813919123162824586<br />
17866458359124566529476545682848912883142607690042<br />
24219022671055626321111109370544217506941658960408<br />
07198403850962455444362981230987879927244284909188<br />
84580156166097919133875499200524063689912560717606<br />
05886116467109405077541002256983155200055935729725<br />
71636269561882670428252483600823257530420752963450</p>
<p>Silahkan kalo mau menghitung sendiri, berapa nilai maksimalnya <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Tetapi berikut ini akan saya berikan program untuk menghitungnya dengan menggunakan bahasa pemrograman java.</p>
<pre class="brush: java;">
import java.io.BufferedReader;
import java.io.FileReader;

/**
 *
 * @author Windu Purnomo
 */
public class Euler8 {
    public static void main(String[] args) {
        String temp;
        int num, mul = 1, maks=1;
        try{
            BufferedReader reader = new BufferedReader(new FileReader(&#34;1000.txt&#34;));
            if(!(temp=reader.readLine()).equals(null)){
                for(int i=0; i&#60;temp.length()-4; i++){
                    for(int j=i; j&#60;i+5; j++){
                        Character c = new Character(temp.charAt(j));
                        String s = c.toString();
                        num = Integer.parseInt(s);
                        mul *= num;
                    }
					//System.out.println(&#34;Perkalian lima ke-&#34;+i+&#34; &#34;+mul);
                    if(maks&#60;mul) maks = mul;
					num = 0;
					mul = 1;
                }
            }
            reader.close();
        }catch(Exception e){
                System.out.println(&#34;gagal&#34;);
        }
        System.out.println(&#34;Nilai terbesar: &#34;+ maks);
    }
}
</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[N-th Prime Number]]></title>
<link>http://windupurnomo.wordpress.com/2009/11/23/n-th-prime-number/</link>
<pubDate>Mon, 23 Nov 2009 04:42:01 +0000</pubDate>
<dc:creator>windupurnomo</dc:creator>
<guid>http://windupurnomo.wordpress.com/2009/11/23/n-th-prime-number/</guid>
<description><![CDATA[Example problem: What is 6th prime number? Answer: By listing the first six prime numbers: 2, 3, 5, ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Example problem:<br />
What is 6th prime number?<br />
Answer:<br />
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6-th prime is 13</p>
<p>Now, what is 10001-th prime number?</p>
<p>It is simple, if we use program to count it:</p>
<pre class="brush: cpp;">
/*
  Name: N-th Prime Number
  Copyright: 2009
  Author: Windu Purnomo
  Date: 23/11/09 11:40
  Description: Get n-th prime number, after list it.
*/

#include&#60;stdio.h&#62;
main(){
    int i, j, n;
    int flag = 1;
    int counter = 0;

    scanf(&#34;%d&#34;, &#38;n);
    for(i=2; i&#62;0; i++){
       for(j=2; j&#60;i; j++){
          if(i%j==0){
             flag = 0;
             break;
          }
       }
       if(flag){
          counter++;
          if(counter == n){
             printf(&#34;It is %d-th prime number: %d&#34;, n, i);
             break;
          }
       }else{
          flag = 1;
       }
    }
    getch();
    return 0;
}
</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 13]]></title>
<link>http://vilhena.wordpress.com/2009/11/21/problem-13/</link>
<pubDate>Sat, 21 Nov 2009 17:59:33 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/11/21/problem-13/</guid>
<description><![CDATA[For the 13th problem of the Euler project, I started by defining a list containing the one hundred n]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>For the <a href="http://projecteuler.net/index.php?section=problems&#38;id=13" target="_blank">13th problem</a> of the Euler project, I started by defining a list containing the one hundred numbers. So, it would be something like:</p>
<p><span style="color:green;"><span style="text-decoration:underline;">import</span></span> Char</p>
<p><span style="color:blue;">l</span> <span style="color:red;">=</span> <span style="color:red;">[</span><span style="color:#ff00ff;">37107287533902102798797998220837590246510135740250</span><span style="color:#a52a2a;">,</span></p>
<p><span style="color:#ff00ff;">46376937677490009712648124896970078050417018260538</span><span style="color:#a52a2a;">,</span></p>
<p>...</p>
<p><span style="color:#ff00ff;">53503534226472524250874054075591789781264330331690</span><span style="color:red;">]</span></p>
<p>Then, it was a matter of defining a small function that would return the first 10 digits of the sum of all those numbers:</p>
<p><span style="color:blue;">sumF</span> <span style="color:red;">::</span> <span style="color:red;">[</span>Integer<span style="color:red;">]</span> <span style="color:red;">-&#62;</span> <span style="color:red;">[</span>Char<span style="color:red;">]</span><br />
<span style="color:blue;">sumF</span> <span style="color:red;">=</span> take <span style="color:#ff00ff;">10</span> <span style="color:#a52a2a;">.</span> show <span style="color:#a52a2a;">.</span> sum</p>
<p>The result (of executing <em>sumF l</em>) was achieved using the following timing/memory resources: (0.01 secs, 5340404 bytes)</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 20]]></title>
<link>http://vilhena.wordpress.com/2009/11/21/problem-20/</link>
<pubDate>Sat, 21 Nov 2009 17:49:08 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/11/21/problem-20/</guid>
<description><![CDATA[The problem number 20 of the Euler project aims to find the sum of the digits in the number 100!. Fi]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The problem <a href="http://projecteuler.net/index.php?section=problems&#38;id=20" target="_blank">number 20</a> of the Euler project aims to find the sum of the digits in the number 100!.</p>
<p>First, I defined the factorial function, as follows:</p>
<p><span style="color:green;"><span style="text-decoration:underline;">import</span></span> Char</p>
<p><span style="color:blue;">fac</span> n <span style="color:red;">=</span> foldr <span style="color:#a52a2a;">(</span><span style="color:#a52a2a;">*</span><span style="color:#a52a2a;">)</span> <span style="color:#ff00ff;">1</span> <span style="color:red;">[</span><span style="color:#ff00ff;">1</span><span style="color:red;">..</span>n<span style="color:red;">]</span></p>
<p>Then, I just defined a function that calculates the factorial of a given number and performs some transformations in order to sum the digits of that number. The function was defined as one can see below:</p>
<p><span style="color:blue;">sumF</span> <span style="color:red;">=</span> sum <span style="color:#a52a2a;">.</span> map digitToInt <span style="color:#a52a2a;">.</span> show <span style="color:#a52a2a;">.</span> fac</p>
<p>Then, by executing <em>sumF 100</em>, one gets the correct result for this problem.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #102]]></title>
<link>http://keyzero.wordpress.com/2009/11/20/project-euler-problem-102/</link>
<pubDate>Fri, 20 Nov 2009 16:46:00 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/20/project-euler-problem-102/</guid>
<description><![CDATA[Read the details of the problem here Summary For how many triangles in the text file does the interi]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=102" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>For how many triangles in the text file does the interior contain the origin?</p>
<p><strong>Solution</strong></p>
<p>This question had me wondering for a while as to what the best approach to take might be. In the end I went for a simple approach that just relied on the fact that the interior angles from any point within a triangle to the vertexes would add up to 360° or 2 * Pi radians. To calculate the angles at the origin I relied on some things I’d learned when I used to write computer games as a hobby.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Dot_product" target="_blank">dot product</a> of two 2D vectors, U and V, is equivalent to the following two formulae:</p>
<p>U•V = U<sub>0</sub>V<sub>0</sub> + U<sub>1</sub>V<sub>1</sub></p>
<p>U•V = &#124;U&#124; * &#124;V&#124; * cos θ</p>
<p>Where &#124;U&#124; is the magnitude of the vector U, i.e. sqrt( U<sub>0</sub>² + U<sub>1</sub>² ) </p>
<p>So this gives: cos θ = ( U<sub>0</sub>V<sub>0</sub> + U<sub>1</sub>V<sub>1</sub> ) / ( &#124;U&#124; * &#124;V&#124; )</p>
<p>The coded solution is then quite trivial and just calculates the angle at the origin subtended by vectors out to the pairs of vertices for each line segment.</p>
<pre>new File(&#34;euler_P102_triangles.txt&#34;).eachLine {
    def (x0, y0, x1, y1, x2, y2) = it.split(&#34;,&#34;)*.toInteger()
    def ( a, b, c ) = [ [ x0, y0 ], [ x1, y1 ], [ x2, y2 ] ]
    def theta = 0.0

    [ [ a, b ], [ a, c ], [ b, c ] ].each {
        def ( u, v ) = it
        def dot = u[0] * v[0] + u[1] * v[1]
        def ul =  Math.sqrt(u[0] * u[0] + u[1] * u[1])
        def vl =  Math.sqrt(v[0] * v[0] + v[1] * v[1])
        theta += Math.acos(dot / (ul * vl))
    }

    if (Math.abs(theta - 2 * Math.PI) &#60; 1E-7) answer++
}</pre>
<p>It ran in 0.96 seconds and wasn’t difficult to write once I’d decided what approach to take! Note the importance of using an epsilon value (1E-7) when comparing floating-point numbers as you can’t rely on an exact equality.</p>
<p>There are probably quicker ways to do this (in fact, I know there are) but this approach was nice &#38; straightforward to code off the top of my head and ran in an acceptable time.</p>
<p><strong>Conclusion</strong></p>
<p>Groovy makes reading a text file and parsing it into appropriate data structures quite simple. It didn’t really make any difference to the rest of the solution apart from perhaps the nice syntax on the &#60;list of sides&#62;.each{} loop as it was mostly just straightforward maths.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Solution: Problem 6 in F Sharp]]></title>
<link>http://mmsmatt.wordpress.com/2009/11/18/project-euler-solution-problem-6-in-f-sharp/</link>
<pubDate>Wed, 18 Nov 2009 23:03:30 +0000</pubDate>
<dc:creator>mmsmatt</dc:creator>
<guid>http://mmsmatt.wordpress.com/2009/11/18/project-euler-solution-problem-6-in-f-sharp/</guid>
<description><![CDATA[The challenge The short version: Find the difference between the sum of the squares of the first one]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=6">The challenge</a></p>
<p>The short version:</p>
<blockquote><p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p></blockquote>
<p>Today&#8217;s solution is a one-liner, I ran it in the interactive environment:</p>
<pre class="brush: plain;">
[1I .. 100I] &#124;&#62; (fun nums -&#62; List.sum nums ** 2I - (List.map (fun x -&#62; x ** 2I) nums &#124;&#62; List.sum));;
</pre>
<p>I pass a list of the bigints 1 through 100 into an anonymous function. The function squares the list&#8217;s sum, and then subtracts the sum of all of the squares of the list elements.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #99]]></title>
<link>http://keyzero.wordpress.com/2009/11/18/project-euler-problem-99/</link>
<pubDate>Wed, 18 Nov 2009 18:22:04 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/18/project-euler-problem-99/</guid>
<description><![CDATA[Read the details of the problem here Summary Which base/exponent pair in the file has the greatest n]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=99" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>Which base/exponent pair in the file has the greatest numerical value?</p>
<p><strong>Solution</strong></p>
<p>I took the straightforward approach to this one. As the resulting numbers would be huge it was just a matter of using log10(base) * exp and comparing those values. The coded solution is trivial.</p>
<pre>def ( i, answer, max ) = [ 1, -1, 0.0 ]

new File(&#34;base_exp.txt&#34;).eachLine {
    def ( base, exp ) = it.split(&#34;,&#34;)*.toDouble()
    def log = Math.log10(base) * exp
    if (log &#62; max) ( answer, max ) = [ i, log ]
    i++
}</pre>
<p>It ran in 46 ms and took a very short time to write!</p>
<p><strong>Conclusion</strong></p>
<p>Groovy was a very nice language to use for this. It was very easy to read the file and process the contents. See how the <em>spread-dot</em> operator is used on the list resulting from the <em>split</em> to convert both operands in to Doubles ready for the next step. The only slightly irritating part was that I had to maintain my own record count as there is no <em>eachWithIndex()</em> method available on a File object. I could have treated this differently by reading into a list first but this seemed quicker!</p>
<p>As there was no heavy maths processing performance was fine, it was quick to write and the code intent is clear.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 16]]></title>
<link>http://vilhena.wordpress.com/2009/11/14/problem-16/</link>
<pubDate>Sat, 14 Nov 2009 14:39:10 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/11/14/problem-16/</guid>
<description><![CDATA[The 16th problem of the project Euler site was fairly easy to solve. With a one-line function, I am ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The <a href="http://projecteuler.net/index.php?section=problems&#38;id=16" target="_blank">16th problem</a> of the project Euler site was fairly easy to solve.</p>
<p>With a one-line function, I am able to calculate the sum of the digits of <em>2</em><sup><em>x</em></sup>, for a given<em> x</em> as input:</p>
<p><span style="color:green;"><span style="text-decoration:underline;">import</span></span> Char</p>
<p><span style="color:blue;">sumF</span> <span style="color:red;">=</span> sum <span style="color:#a52a2a;">.</span> map digitToInt <span style="color:#a52a2a;">.</span> show <span style="color:#a52a2a;">.</span> <span style="color:#a52a2a;">(</span><span style="color:#a52a2a;">^</span><span style="color:#a52a2a;">)</span> <span style="color:#ff00ff;">2</span></p>
<p>Then, it was just a matter of calling the function with 1000 as input. The execution was really fast, so ghci presented (0.00 secs, 0 bytes).</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Solution: Problem 2 in F Sharp]]></title>
<link>http://mmsmatt.wordpress.com/2009/11/10/project-euler-solution-problem-2-in-f-sharp/</link>
<pubDate>Tue, 10 Nov 2009 20:32:05 +0000</pubDate>
<dc:creator>mmsmatt</dc:creator>
<guid>http://mmsmatt.wordpress.com/2009/11/10/project-euler-solution-problem-2-in-f-sharp/</guid>
<description><![CDATA[The challenge Each new term in the Fibonacci sequence is generated by adding the previous two terms.]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=2">The challenge</a></p>
<blockquote><p>
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:</p>
<p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</p>
<p>Find the sum of all the even-valued terms in the sequence which do not exceed four million.
</p></blockquote>
<p>We will use lazily-evaluated sequences in F# to solve this problem. First, we define the Fibonacci series:</p>
<pre class="brush: plain;">
let fibs =
    let rec loop a b = seq { yield a; yield! loop b (a+b) }
    loop 1I 2I
</pre>
<p>Next, we define a simple test for the even-ness of a bigint:</p>
<pre class="brush: plain;">
let eventest = function
    &#124; x when (x % 2I = 0I) -&#62; true
    &#124; _ -&#62; false
</pre>
<p>Now we can solve for the sum! We&#8217;ll use the pipeline operator to pass sequences between functions. Also note the anonymous function passed to Seq.takeWhile &#8211; its purpose is to stop computing new values in the fibs sequence when we reach 4,000,000</p>
<pre class="brush: plain;">
let main() =
    Seq.takeWhile (fun x -&#62; x &#60;= 4000000I) fibs
    &#124;&#62; Seq.filter eventest
    &#124;&#62; Seq.sum
    &#124;&#62; printfn &#34;Answer: %A&#34;
</pre>
<p><a href="http://mattsrepo.googlecode.com/svn/trunk/euler/002_EvenFibs.fs">Get the full F# source</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler: Programming (and Math!) Practice]]></title>
<link>http://programmagic.wordpress.com/2009/11/07/project-euler-programming-and-math-practice/</link>
<pubDate>Sat, 07 Nov 2009 22:54:30 +0000</pubDate>
<dc:creator>ddouthitt</dc:creator>
<guid>http://programmagic.wordpress.com/2009/11/07/project-euler-programming-and-math-practice/</guid>
<description><![CDATA[As one who loved mathematics (and still do &#8211; I considered becoming a mathematician) &#8211; Pr]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>As one who loved mathematics (and still do &#8211; I considered becoming a mathematician) &#8211; <a href="http://projecteuler.net/">Project Euler</a> is a beautiful place to try and get practice on new programming languages as well as mathematical principles.  Project Euler provides problems that require solutions, and which are designed for less than one minute solutions using the computer.  The problems also highlight a mathematical principle in their solution.</p>
<p>I&#8217;ve only worked on a very few problems so far, but it appears that it is quite easy to write a brute force programming algorithm for some of these problems; personally, I am attempting to find the mathematical solution as well as solving the problem in <a href="http://www.scala-lang.org/">Scala</a>.  There is a nice <a href="http://scala-blogs.org/2007/12/project-euler-fun-in-scala.html">article</a> about one person using Scala to solve Project Euler problems.</p>
<p>The biggest problem in using Project Euler for learning a programming language is that Project Euler isn&#8217;t set up for multiple language users: each user is able to specify (and track) solutions in only one language &#8211; but this is not a problem really.</p>
<p>If you want to solve more mathematical problems (not necessarily with a computer), the <a href="http://mathschallenge.net/index.php?section=home">MathsChallenge</a> web site is an excellent one, and is apparently the predecessor to the Project Euler site.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #5]]></title>
<link>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-5/</link>
<pubDate>Sat, 07 Nov 2009 15:18:31 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-5/</guid>
<description><![CDATA[Read the details of the problem here Summary What is the smallest number that is evenly divisible by]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=5" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?</p>
<p><strong>Solution</strong></p>
<p>I didn’t need to write a program for this question. I vaguely remembered from school that the LCM of a series of numbers is just the product of the maximum powers of its prime factors, or something like that terminology.</p>
<p>It goes something like this. 2^4 = 16, which is less than 20, so that’s included but 2^5 isn’t as 32 is greater than 20. Similarly 3^2 = 9, is in, but 3^3 = 27 is out. Intermediate numbers will always be able to be factored into products of lesser powers of the primes and can therefore be ignored.</p>
<p>So this gives the list: 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19</p>
<p>So it wasn’t really much of a program to be written though <strong><em>groovysh</em></strong> did the calculation admirably. Performance wasn’t an issue!</p>
<p><strong>Conclusion</strong></p>
<p>Just goes to show it’s sometimes worth engaging brain before hitting the keyboard. </p>
<p>Reading ahead in the Project Euler questions it looks like factorization of large numbers is an ongoing theme so I’ll probably end up writing something that does this but I’ll do that when I need it.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #59]]></title>
<link>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-59/</link>
<pubDate>Sat, 07 Nov 2009 14:53:19 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-59/</guid>
<description><![CDATA[Read the details of the problem here Summary Using a brute force attack, can you decrypt the cipher ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=59" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>Using a brute force attack, can you decrypt the cipher using XOR encryption?</p>
<p><strong>Solution</strong></p>
<p>I actually got lucky with this one. The clue was that it contained English text, so the most common character was likely to be SPACE (ASCII 32). I tried for a simple frequency analysis first on that basis to find candidate key characters and it gave the right answer. It ran in 0.62 seconds.</p>
<p>If this approach hadn’t worked my next approach would have been to restrict the key characters as per the question and compare each decoded sequence against a valid character set e..g a-z, A-Z, comma, parentheses, full-stops, etc. looking for least exceptions and rating the key against that.</p>
<p>For something like this, where the plaintext type and key length was known it didn’t really warrant coding up a sliding XOR attack.</p>
<pre>def seqs = [ [], [], [] ]

new File(&#34;euler_p59_cipher1.txt&#34;).text.split(&#34;,&#34;)
        .eachWithIndex { val, idx –&#62;
            seqs[idx % seqs.size()] &#60;&#60; val.toInteger()
        }

def answer = 0
def ASCII_SPACE = 32

seqs.each { vals -&#62;

    def freqs = new int[256]
    vals.each { freqs[it]++ }

    def ( pkey, max ) = [ 0, 0 ]
    freqs.eachWithIndex { freq, chr -&#62;
        if (freq &#62; max) ( pkey, max ) = [ chr ^ ASCII_SPACE, freq ]
    }

    answer += vals.sum { it ^ pkey }
}</pre>
<p>I went on to decode the passage and the result was truly Biblical. Not sure that I would have used that particular text in such a website but, as this solution proves, you don’t actually need to read it to get the answer. As to the key itself, well, I’d have stayed clear of that too. It’s one of those passwords you just NEVER use!</p>
<p><strong>Conclusion</strong></p>
<p>Groovy was a very nice language to use for this. It made it very easy to read the file and split it into sequences for each character in the keyword. In fact, that could have been a one-liner. The construction of the frequency table and the extraction of a potential key character was also very straightforward.</p>
<p>As there was no heavy maths processing performance was fine, it was quick to write and the code intent is clear.</p>
<p>It also helped that I’ve had an interest in cryptography since young. I thoroughly recommend Simon Singh’s <a href="http://www.simonsingh.net/The_Code_Book.html" target="_blank">Code Book</a> as a gentle introduction into the topic.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #4]]></title>
<link>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-4/</link>
<pubDate>Sat, 07 Nov 2009 01:00:37 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/07/project-euler-problem-4/</guid>
<description><![CDATA[Read the details of the problem here Summary Find the largest palindrome made from the product of tw]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=4" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>
<p><strong>Solution</strong></p>
<p>An obvious brute force solution completes in 1.14 seconds. The only simple optimizations I made were to count down rather than up for the two numbers and to put a guard condition on the expensive functions.</p>
<pre>String.metaClass.isPalindrome = { -&#62;
    delegate == delegate.reverse()
}

def answer = 0

for (i in 999..100) {
    for (j in 999..100) {
       def p = i * j
       if ((p &#62; answer)  &#38;&#38; (p.toString().isPalindrome())) answer = p
    }
}
</pre>
<p>As they say, &#8220;&#8216;Nuff Said!&#8221;</p>
<p><strong>Conclusion</strong></p>
<p>The ability to extend to the String metaclass with an “isPalindrome()” method leads to quite a nice syntax but isn’t much different to just defining a support method. (I could also have put the function into the Integer class directly and hidden the String conversion in that.)</p>
<p>The solution ran well within my 15 second limit but still felt slow for what it was doing. An equivalent Java version runs in 137 ms. That’s around 8x faster.</p>
</p>
<pre></pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Solution: Problem 1 in F Sharp]]></title>
<link>http://mmsmatt.wordpress.com/2009/11/05/project-euler-solution-problem-1-in-f-sharp/</link>
<pubDate>Fri, 06 Nov 2009 04:47:42 +0000</pubDate>
<dc:creator>mmsmatt</dc:creator>
<guid>http://mmsmatt.wordpress.com/2009/11/05/project-euler-solution-problem-1-in-f-sharp/</guid>
<description><![CDATA[I discovered F# while searching for introductions to functional programming. Later that day I discov]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I discovered F# while searching for introductions to functional programming. Later that day I discovered <a href="http://projecteuler.net/index.php?section=about">Project Euler</a>, a set of mathematical challenges for programmers. So with a new language and new problems, let&#8217;s start from the top.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#38;id=1">The challenge<br />
</a></p>
<blockquote>
<div>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p>
</div>
</blockquote>
<p>My first solution sticks to familiar if/else conditionals:</p>
<pre class="brush: plain;">
let rec AddIfElse ceiling current sum =
    if current &#62;= ceiling then
        sum
    else
        if current % 3 = 0 &#124;&#124; current % 5 = 0 then
            AddIfElse ceiling (current+1) (sum+current)
        else
            AddIfElse ceiling (current+1) sum
</pre>
<p>My second solution uses F# <a href="http://en.wikibooks.org/wiki/F_Sharp_Programming/Pattern_Matching_Basics">pattern matching</a> and a nested function, just to play with the language:</p>
<pre class="brush: plain;">
let rec AddPatterns ceiling current sum =
    let adjustment = function
        &#124; x when x % 3 = 0 &#124;&#124; x % 5 = 0 -&#62; x
        &#124; _ -&#62; 0

    match current with
        &#124; x when x = ceiling -&#62; sum
        &#124; _ -&#62; AddPatterns ceiling (current+1) (sum + adjustment current) 
</pre>
<p>In functional programming, functions are values and can be manipulated as such. Let&#8217;s set up a function that hides the recursion in our two solutions, so a client won&#8217;t have to initialize the recursive parameters. Our new function accepts:</p>
<ul>
<li>A number to solve for (1000 to answer the Euler question)</li>
<li>A function to use in solving (either the IfElse or Patterns variant)</li>
</ul>
<p>This function will return the answer computed by the function passed in.</p>
<pre class="brush: plain;">
let Solve n fn =
    fn n 1 0
</pre>
<p>Now we can solve the problem!</p>
<pre class="brush: plain;">
let main() =
    printfn &#34;If/else solution:&#34;
    printfn &#34;10:     %d&#34; (Solve 10 AddIfElse)
    printfn &#34;1000:   %d&#34; (Solve 1000 AddIfElse)
    printfn &#34;Pattern matching solution:&#34;
    printfn &#34;10:     %d&#34; (Solve 10 AddPatterns)
    printfn &#34;1000:   %d&#34; (Solve 1000 AddPatterns)
</pre>
<p><a href="http://mattsrepo.googlecode.com/svn/trunk/euler/001_AddNums.fs">Get the full F# source</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 9]]></title>
<link>http://vilhena.wordpress.com/2009/11/05/problem-9/</link>
<pubDate>Thu, 05 Nov 2009 19:34:18 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/11/05/problem-9/</guid>
<description><![CDATA[In this post I will show how did I solve the 9th problem of the Euler project. This was one of the f]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>In this post I will show how did I solve the <a href="http://projecteuler.net/index.php?section=problems&#38;id=9" target="_blank">9th problem</a> of the Euler project. This was one of the first problems that gave me performance issues and therefore a brute force approach was not good enough. However, I just realised that after building a brute force algorithm <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I started describing all the conditions stated in the description of the problem. Therefore, I got this Haskell function:</p>
<p><span style="color:blue;">l</span> <span style="color:red;">=</span> <span style="color:red;">[</span> <span style="color:red;">[</span>a<span style="color:#a52a2a;">,</span>b<span style="color:#a52a2a;">,</span>c<span style="color:red;">]</span> <span style="color:red;">&#124;</span> a <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:red;">1</span><span style="color:red;">..</span><span style="color:red;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> b <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:red;">1</span><span style="color:red;">..</span><span style="color:red;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> c <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:red;">1</span><span style="color:red;">..</span><span style="color:red;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> a<span style="color:#a52a2a;">+</span>b<span style="color:#a52a2a;">+</span>c<span style="color:#a52a2a;">==</span> <span style="color:red;">1000</span> <span style="color:#a52a2a;">&#38;&#38;</span> a<span style="color:#a52a2a;">^</span><span style="color:red;">2</span> <span style="color:#a52a2a;">+</span> b<span style="color:#a52a2a;">^</span><span style="color:red;">2</span> <span style="color:#a52a2a;">==</span> c<span style="color:#a52a2a;">^</span><span style="color:red;">2</span> <span style="color:#a52a2a;">&#38;&#38;</span> a <span style="color:#a52a2a;">&#60;</span> b  <span style="color:#a52a2a;">&#38;&#38;</span> b <span style="color:#a52a2a;">&#60;</span> c<span style="color:red;">]</span></p>
<p>Then, with a simple function called <em>value:</em></p>
<p><span style="color:blue;">value</span> <span style="color:red;">=</span> product <span style="color:#a52a2a;">.</span> head</p>
<p>By executing <em>value l</em>, I reached the following result: (371.90 secs, 32855979636 bytes)</p>
<p>As you can see, it is not an interesting result for such a problem. I was just making too many combinations of <em>a</em>, <em>b</em> and <em>c</em>, and most of the conditions were unnecessary. From the problem description, it is possible to deduct that <em>c</em> can be written by means of <em>a</em> and <em>b</em>. Using this simple substitution and removing the unnecessary conditions, I reached this solution:</p>
<p><span style="color:blue;">h</span> <span style="color:red;">=</span> <span style="color:red;">[</span><span style="color:red;">[</span>a<span style="color:#a52a2a;">,</span>b<span style="color:#a52a2a;">,</span><span style="color:red;">1000</span><span style="color:blue;"><em>-</em></span>a<span style="color:blue;"><em>-</em></span>b<span style="color:red;">]</span> <span style="color:red;">&#124;</span> a <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:red;">1</span><span style="color:red;">..</span><span style="color:red;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> b <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:red;">1</span><span style="color:red;">..</span><span style="color:red;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span>a<span style="color:#a52a2a;">^</span><span style="color:red;">2</span> <span style="color:#a52a2a;">+</span> b<span style="color:#a52a2a;">^</span><span style="color:red;">2</span> <span style="color:#a52a2a;">==</span> <span style="color:#a52a2a;">(</span><span style="color:red;">1000</span><span style="color:blue;"><em>-</em></span>a<span style="color:blue;"><em>-</em></span>b<span style="color:#a52a2a;">)</span><span style="color:#a52a2a;">^</span><span style="color:red;">2</span><span style="color:red;">]</span></p>
<p>After running <em>value h</em>, this was the result: (0.79 secs, 108452856 bytes), which is much better than the first one.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Problem #3]]></title>
<link>http://keyzero.wordpress.com/2009/11/05/project-euler-problem-3/</link>
<pubDate>Thu, 05 Nov 2009 01:15:08 +0000</pubDate>
<dc:creator>keyzero</dc:creator>
<guid>http://keyzero.wordpress.com/2009/11/05/project-euler-problem-3/</guid>
<description><![CDATA[Read the details of the problem here Summary What is the largest prime factor of the number 60085147]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://projecteuler.net/index.php?section=problems&#38;id=3" target="_blank">Read the details of the problem here</a></p>
<p><strong>Summary</strong></p>
<p>What is the largest prime factor of the number 600851475143?</p>
<p><strong>Solution</strong></p>
<p>Pretty straightforward question with a straightforward brute force answer.</p>
<p>The code simply uses a naïve Sieve of Eratosthenes to generate primes up to the&#160; square root of the target number “n” and then factorises from that sieve.</p>
<pre>long n = 600851475143L
def isPrime = new boolean[Math.sqrt(n)+1]
Arrays.fill(isPrime, true);
isPrime[1] = false

for (int i in 2..Math.sqrt(isPrime.size())) {
    if (isPrime[i]) {
        for (j = i + i; j &#60; isPrime.size(); j += i) isPrime[j] = false
    }
}

def (answer, i) = [ 0, 2 ]
while (n &#62; 1) {
    if ((isPrime[i]) &#38;&#38; (n % i == 0)) {
        ( n, answer ) = [ n / i, i ]
    }
    i++
}</pre>
<p>It’s all very quick to write but look at the explicit typing in the declaration of “n”. Without this the “n / i” calculation in the factorisation loop will assign a BigDecimal to n which will then fail on the next mod operation. It is also possible to work around this feature by casting the result but the typing approach is probably the lesser of two evils.</p>
<p>The script completes in 3.44 seconds, the sieve creation itself taking 3.36 seconds. This seems extremely slow. Porting this over into classic Java it returns in 32 ms with the sieve creation taking 31 ms. That’s 100 times faster.</p>
<p>A simpler solution, without the sieve and relying on the BigInteger#isProbablePrime method returns in 1.21 secs, whereas the Java version of this takes 0.87 secs.</p>
<p><strong>Conclusion</strong></p>
<p>Groovy didn’t actually add much to this solution apart from removing the boilerplate class code that Java required (and that IDEs generate automatically so it’s not much of a saving). The multiple assignment syntax was nice but nothing that I couldn’t have done without.</p>
<p>The fact that doing calculations like this means that the developer has to be aware of the types of the variables isn’t really a big deal but does suddenly make you aware that you’re not running in a real typeless environment like Ruby or Python. The runtime error message I received, “<em>Cannot use mod() on this number type: BigDecimal”</em> was a bit of a surprise and emphasises how much testing is necessary with dynamically typed languages where the number types can differ in the operations they support.</p>
<p>It appears from this that Groovy is imposing quite a severe overhead, adding some two orders of magnitude to numerical processing. If I need to do any real number crunching it’ll be best to create a proper Java helper class and use it from the Groovy script.</p>
<p>For the BigInteger#isProbablePrime solution (shown below), Groovy does provide for a much cleaner syntax than the Java version in which it’s obvious that the BigInteger class has been grafted on and, without operator overloading being available, is a bit of a stranger in a strange land.</p>
<pre>long n = 600851475143G
def (answer, i) = [ 0, 2G ]
while (n &#62; 1) {
    if (i.isProbablePrime(100) &#38;&#38; (n % i == 0)) {
        ( n, answer ) = [ n / i, i ]
    }
    i++
}</pre>
<p>See how easy it is to make “i” a BigInteger simply by appending a “G” to the value assignment. So it’s still necessary to be aware of the type, but this makes it easier than having to explicitly “new” a BigInteger.</p>
<p>The timings for this non-sieve solution show that, for large numbers of primes to be checked it’s almost always preferable to use a sieve in Java, whereas in Groovy there’s a higher tolerance given the overhead of building that sieve.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Project Euler Solution: Problem 1]]></title>
<link>http://mmsmatt.wordpress.com/2009/11/05/euler-project-solution-problem-1/</link>
<pubDate>Thu, 05 Nov 2009 00:15:03 +0000</pubDate>
<dc:creator>mmsmatt</dc:creator>
<guid>http://mmsmatt.wordpress.com/2009/11/05/euler-project-solution-problem-1/</guid>
<description><![CDATA[Today I ran into Project Euler care of Stack Overflow, and decided to try my hand. I&#8217;m looking]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Today I ran into<a href="http://projecteuler.net/index.php?section=about"> Project Euler</a> care of Stack Overflow, and decided to try my hand. I&#8217;m looking for excuses to play with F#, and this works great.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#38;id=1">First problem</a>:</p>
<blockquote>
<div>
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p>
</div>
</blockquote>
<p>Two solutions here &#8211; both more general than Euler requests. Note the nested functions and use of recursion in both. One uses familiar if/else conditionals:</p>
<pre class="brush: plain;">
let AddNums n =
    let rec add ceiling current sum =
        if current &#38;gt;= ceiling then
            sum
        else
            if current % 3 = 0 &#124;&#124; current % 5 = 0 then
                add ceiling (current+1) (sum+current)
            else
                add ceiling (current+1) sum

    add n 1 0
</pre>
<p>The next solution uses F# pattern matching and guards. It&#8217;s functionally equivalent to the first, but highlights some F# power:</p>
<pre class="brush: plain;">[/sourcecode]</pre>
<p>Source code is<a href="http://code.google.com/p/mattsrepo/source/browse/trunk/euler/001_AddNums.fs"> here at Google Code</a>.</p>
<p>&#160;</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 8]]></title>
<link>http://vilhena.wordpress.com/2009/11/04/problem-8/</link>
<pubDate>Wed, 04 Nov 2009 15:52:39 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/11/04/problem-8/</guid>
<description><![CDATA[The 8th problem in the Project Euler website didn&#8217;t offer a lot of resistance. I started by de]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The <a href="http://projecteuler.net/index.php?section=problems&#38;id=8" target="_blank">8th problem</a> in the Project Euler website didn&#8217;t offer a lot of resistance.</p>
<p>I started by defining the 1000-digit number in Haskell, naming it n. So, I had something like:</p>
<p><span style="color:green;"><span style="text-decoration:underline;">import</span></span> Char<br />
<span style="color:green;"><span style="text-decoration:underline;">import</span></span> List</p>
<p><span style="color:blue;">n</span> <span style="color:red;">::</span> Integer<br />
<span style="color:blue;">n</span> <span style="color:red;">=</span> <span style="color:#ff00ff;">73167176531 &#8230; 52963450</span></p>
<p>(I didn&#8217;t see the point of writing the entire number here, since it is very big). Afterwards, it was a matter of defining this simple function:</p>
<p><span style="color:blue;">f</span> <span style="color:red;">::</span> Integer <span style="color:red;">-&#62;</span> Int<br />
<span style="color:blue;">f</span> <span style="color:red;">=</span> head <span style="color:#a52a2a;">.</span> reverse <span style="color:#a52a2a;">.</span> sort <span style="color:#a52a2a;">.</span> g <span style="color:#a52a2a;">.</span> map digitToInt <span style="color:#a52a2a;">.</span> show</p>
<p>Where g is defined as:</p>
<p><span style="color:blue;">g</span> <span style="color:#a52a2a;">(</span>x1<span style="color:red;"><strong>:</strong></span>x2<span style="color:red;"><strong>:</strong></span>x3<span style="color:red;"><strong>:</strong></span>x4<span style="color:red;"><strong>:</strong></span>x5<span style="color:red;"><strong>:</strong></span>xs<span style="color:#a52a2a;">)</span> <span style="color:red;">=</span> x1<span style="color:#a52a2a;">*</span>x2<span style="color:#a52a2a;">*</span>x3<span style="color:#a52a2a;">*</span>x4<span style="color:#a52a2a;">*</span>x5 <span style="color:red;"><strong>:</strong></span> g <span style="color:#a52a2a;">(</span>x2<span style="color:red;"><strong>:</strong></span>x3<span style="color:red;"><strong>:</strong></span>x4<span style="color:red;"><strong>:</strong></span>x5<span style="color:red;"><strong>:</strong></span>xs<span style="color:#a52a2a;">)</span><br />
<span style="color:blue;">g</span> <span style="color:green;"><span style="text-decoration:underline;">_</span></span> <span style="color:red;">=</span> []</p>
<p>You can argue that it could be more efficient, however my ghci calculates the following execution time:</p>
<p>(0.03 secs, 1834872 bytes)</p>
<p>Which is good enough <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 4]]></title>
<link>http://vilhena.wordpress.com/2009/10/31/problem-4/</link>
<pubDate>Sat, 31 Oct 2009 21:43:19 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/10/31/problem-4/</guid>
<description><![CDATA[I could see that the problem 4 of the Euler project could be solve with a brute force function. Sinc]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I could see that the <a href="http://projecteuler.net/index.php?section=problems&#38;id=4" target="_blank">problem 4</a> of the Euler project could be solve with a brute force function.</p>
<p>Since one needed to calculate the product of two 3-digit numbers, I created two lists of all possible 3-digit numbers, and tried all combinations. Here is my implementation:</p>
<p><span style="color:green;"><span style="text-decoration:underline;">import</span></span> List</p>
<p><span style="color:blue;">palindrome</span> <span style="color:red;">=</span> <span style="color:red;">[</span> n<span style="color:#a52a2a;">*</span>m <span style="color:red;">&#124;</span> n <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:#ff00ff;">100</span><span style="color:red;">..</span><span style="color:#ff00ff;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> m <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:#ff00ff;">100</span><span style="color:red;">..</span><span style="color:#ff00ff;">999</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> show <span style="color:#a52a2a;">(</span>n<span style="color:#a52a2a;">*</span>m<span style="color:#a52a2a;">)</span> <span style="color:#a52a2a;">==</span> reverse <span style="color:#a52a2a;">(</span>show <span style="color:#a52a2a;">(</span>n<span style="color:#a52a2a;">*</span>m<span style="color:#a52a2a;">)</span><span style="color:#a52a2a;">)</span> <span style="color:red;">]</span></p>
<p><span style="color:blue;">lpal</span> <span style="color:red;">=</span> <span style="color:#a52a2a;">(</span>last <span style="color:#a52a2a;">.</span> sort<span style="color:#a52a2a;">)</span> palindrome</p>
<p>GHCi took about 2 seconds (and a lot of bytes!) in my computer to figure out the result.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 5]]></title>
<link>http://vilhena.wordpress.com/2009/10/31/problem-5/</link>
<pubDate>Sat, 31 Oct 2009 21:32:37 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/10/31/problem-5/</guid>
<description><![CDATA[The 5th problem of the Project Euler was the first one that gave me some work, mainly because I star]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The <a href="http://projecteuler.net/index.php?section=problems&#38;id=5" target="_blank">5th problem</a> of the Project Euler was the first one that gave me some work, mainly because I started (again) by trying a brute force algorithm. Of course, I had problems with it, starting by not getting the solution at all!! It just took too much time to calculate it and I did not let it finish. I tried different values as input, but it was not enough. Here is the code I used:</p>
<p><span style="color:blue;">f</span> [] <span style="color:red;">=</span> <span style="color:#ff00ff;">0</span><br />
<span style="color:blue;">f</span> <span style="color:#a52a2a;">(</span>x<span style="color:red;"><strong>:</strong></span>xs<span style="color:#a52a2a;">)</span> <span style="color:red;">=</span> <span style="color:green;"><span style="text-decoration:underline;">if</span></span> condition x <span style="color:green;"><span style="text-decoration:underline;">then</span></span> x <span style="color:green;"><span style="text-decoration:underline;">else</span></span> f xs</p>
<p><span style="color:blue;">condition</span> <span style="color:red;">::</span> Int <span style="color:red;">-&#62;</span> Bool<br />
<span style="color:blue;">condition</span> <span style="color:red;">=</span> foldr <span style="color:#a52a2a;">(</span><span style="color:#a52a2a;">&#38;&#38;</span><span style="color:#a52a2a;">)</span> True <span style="color:#a52a2a;">.</span> apply<br />
<span style="color:green;"><span style="text-decoration:underline;">where</span></span> apply n <span style="color:red;">=</span> <span style="color:red;">[</span> mod n x <span style="color:#a52a2a;">==</span> <span style="color:#ff00ff;">0</span> <span style="color:red;">&#124;</span> x <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:#ff00ff;">1</span><span style="color:red;">..</span><span style="color:#ff00ff;">20</span><span style="color:red;">]</span><span style="color:red;">]</span></p>
<p>As you can see, I needed to give a list as input for the function f and be lucky enough to have the correct value within that list. After a few tries, I decided to think a bit more in the problem and searching a bit in the Haskell Prelude and I came across with the lcm function, which allowed me to solve this problem very efficiently:</p>
<p><span style="color:blue;">g</span> <span style="color:red;">=</span> foldr lcm <span style="color:#ff00ff;">1</span> <span style="color:red;">[</span><span style="color:#ff00ff;">1</span><span style="color:red;">..</span><span style="color:#ff00ff;">20</span><span style="color:red;">]</span></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 6]]></title>
<link>http://vilhena.wordpress.com/2009/10/31/problem-6/</link>
<pubDate>Sat, 31 Oct 2009 21:09:12 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/10/31/problem-6/</guid>
<description><![CDATA[The problem 6 of the Euler project site was again easy to solve. Using Haskell, I wrote the followin]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The <a href="http://projecteuler.net/index.php?section=problems&#38;id=6" target="_blank">problem 6</a> of the Euler project site was again easy to solve. Using Haskell, I wrote the following one-line function:</p>
<p><font color="Blue">g</font> <font color="Red">=</font> <font color="Brown">(</font>sum <font color="Red">[</font><font color="Magenta">1</font><font color="Red">..</font><font color="Magenta">100</font><font color="Red">]</font> <font color="Brown">^</font> <font color="Magenta">2</font><font color="Brown">)</font> <font color="Blue"><i>-</i></font> <font color="Brown">(</font>sum <font color="Brown">(</font>map <font color="Brown">(</font><font color="Brown">^</font><font color="Magenta">2</font><font color="Brown">)</font> <font color="Red">[</font><font color="Magenta">1</font><font color="Red">..</font><font color="Magenta">100</font><font color="Red">]</font><font color="Brown">)</font><font color="Brown">)</font></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 1]]></title>
<link>http://vilhena.wordpress.com/2009/10/31/problem-1/</link>
<pubDate>Sat, 31 Oct 2009 20:58:27 +0000</pubDate>
<dc:creator>carlosvilhena</dc:creator>
<guid>http://vilhena.wordpress.com/2009/10/31/problem-1/</guid>
<description><![CDATA[When I looked to the Project Euler website, I was very excited with the problems, so I decided to jo]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>When I looked to the Project Euler website, I was very excited with the problems, so I decided to join and try to solve as much as I can. Since I enjoy <a href="http://www.haskell.org/" target="_blank">Haskell</a> very much, I decided to try to solve them all with it.</p>
<p>The first problem is an easy one, in my opinion. <a href="http://projecteuler.net/index.php?section=problems&#38;id=1" target="_blank">Here</a> you can see the problem description.</p>
<p>Since Haskell has a powerful list type, I took advantage of that and here it is my one-line solution for this problem:</p>
<p><span style="color:blue;">f</span> <span style="color:red;">=</span> foldr <span style="color:#a52a2a;">(</span><span style="color:#a52a2a;">+</span><span style="color:#a52a2a;">)</span> <span style="color:#ff00ff;">0</span> <span style="color:red;">[</span>a <span style="color:red;">&#124;</span> a <span style="color:red;">&#60;-</span> <span style="color:red;">[</span><span style="color:#ff00ff;">1</span><span style="color:red;">..</span><span style="color:#ff00ff;">1000</span><span style="color:red;">]</span><span style="color:#a52a2a;">,</span> mod a <span style="color:#ff00ff;">3</span> <span style="color:#a52a2a;">==</span> <span style="color:#ff00ff;">0</span> <span style="color:#a52a2a;">&#124;&#124;</span> mod a <span style="color:#ff00ff;">5</span> <span style="color:#a52a2a;">==</span> <span style="color:#ff00ff;">0</span><span style="color:red;">]</span></p>
</div>]]></content:encoded>
</item>

</channel>
</rss>
