<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vlixter.com &#187; project-euler</title>
	<atom:link href="http://vlixter.com/tag/project-euler/feed/" rel="self" type="application/rss+xml" />
	<link>http://vlixter.com</link>
	<description>Programming , Tech, and stuff</description>
	<lastBuildDate>Mon, 30 Aug 2010 14:50:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler :: Problem 2 :: Haskell</title>
		<link>http://vlixter.com/2010/01/02/project-euler-problem-2-haskell/</link>
		<comments>http://vlixter.com/2010/01/02/project-euler-problem-2-haskell/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 14:35:11 +0000</pubDate>
		<dc:creator>radicality</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[project-euler]]></category>

		<guid isPermaLink="false">http://vlixter.com/?p=144</guid>
		<description><![CDATA[This is the second tutorial on solving Project Euler problems using Haskell. Hope you will enjoy! Problem Description: 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: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second tutorial on solving <a href="http://projecteuler.net/" target="_blank">Project Euler</a> problems using Haskell. Hope you will enjoy!</p>
<p><strong>Problem Description:</strong></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>Again, create a new text file and call it euler2.hs</p>
<p>First, we are required to make a function that computes the numbers of the Fibonnacci sequence. From the definition fib(x) = fib(x-1) + fib(x-2), we can easily code this in Haskell.</p>
<p>Here is how it would look:</p>
<pre class="brush:ruby">fib :: Int -&gt; Int
fib 1 = 1
fib 2 = 1
fib x = fib(x-1) + fib(x-2)
</pre>
<p>Now on to test this function. Open up ghci, load the file, and type in &#8220;fib 30&#8243;, which asks for the 30th Fibonacci numbers.  Actually, this will be quite slow, here are my results:</p>
<pre class="brush:ruby">*Euler1&gt; fib 30
832040
(2.46 secs, 172153128 bytes)
</pre>
<p>2.46 seconds to get the 30th number ? Way too slow. What is happening here, is that the computer has to do a lot of counting.</p>
<p>fib(30) = fib(29) + fib(28)</p>
<p>= [fib(28) + fib(27)] + [fib(27) + fib(26) ]</p>
<p>and so on and on, expanding the fib, until the problem is reduced to adding up a whole bunch of 1&#8242;s together, as the first two terms are 1&#8242;s.</p>
<p>So we need a solution, where we don&#8217;t need to recompute fib as many times, so you could call it a form of &#8220;caching&#8221;.</p>
<p>Here is my proposed solution:</p>
<pre class="brush:ruby">fib :: Int -&gt; Int
fib n = table !! n
 where
   table = 0 : 1 : zipWith (+) table (tail table)
</pre>
<p>Whoa, what&#8217;s happening here ? We are creating a &#8220;table&#8221; of the Fibonacci numbers, and then simply calling the &#8220;!!&#8221; (value at index) function to get our desired number.</p>
<p>We define a table as a list, with first element 0, and second element 1, and with the rest of the list being computed recursively using &#8220;zipWith (+)&#8221;.</p>
<p>&#8220;zipWith&#8221; is a function that takes another function, such as the plus function (+), and two lists, and it &#8220;zips&#8221; them together using that operator.</p>
<p>For example, zipWith (+) [1,2,3] [1,1,1] will return the list [2,3,4].</p>
<p>Understanding the above revised fib function is best done by simply expanding the recursive bit. Here it is from my prompt.</p>
<pre class="brush:ruby">*Euler1&gt; zipWith (+) [0,1] [1]
[1]
*Euler1&gt; zipWith (+) [0,1,1] [1,1]
[1,2]
*Euler1&gt; zipWith (+) [0,1,1,2] [1,1,2]
[1,2,3]
*Euler1&gt; zipWith (+) [0,1,1,2,3] [1,1,2,3]
[1,2,3,5]
*Euler1&gt; zipWith (+) [0,1,1,2,3,5] [1,1,2,3,5]
[1,2,3,5,8]
</pre>
<p>So we are seeing how the second parameter of zipWith is our desired sequence. Since Haskell implement lazy evaluation, it will stop computing the list as soon as it reaches &#8220;n&#8221;.</p>
<p>This approach is also much faster than the previous one. See for yourself!</p>
<pre class="brush:ruby">*Euler1&gt; fib 30
832040
(0.00 secs, 528596 bytes)
</pre>
<p>0.00 seconds is quite damn fast, and the solution uses up a lot less memory than the previous one!</p>
<p>Now using our knowledge of list comprehensions from the <a href="http://vlixter.com/2010/01/02/project-euler-problem-1-haskell/" target="_blank">previous tutorial</a>, and the function described above, we can solve problem 2 on Project Euler.</p>
<pre class="brush:ruby">fib n = table !! n
 where
    table = 0 : 1 : zipWith (+) table (tail table)

euler2 = sum[x | x&lt;- takeWhile (&lt;4000000) (map fib [1..]), even(x)]</pre>
<p>Line 5 has a couple new function we aren&#8217;t familiar with, such as takeWhile, map, and even.</p>
<p>&#8220;even&#8221; returns true when its parameter is even, and else it returns false. Hence, &#8220;even(x)&#8221; is one of our conditions for the list comprehension</p>
<p>Now the main part of this is</p>
<pre class="brush:ruby">x &lt;- takeWhile (&lt;4000000) (map fib [1..])
</pre>
<p>Let&#8217;s look at &#8220;map&#8221; first. &#8220;Map&#8221; is a function which takes another function, and a list, and it applies that function to every element of the list.</p>
<p>For example, &#8220;map (+1) [1,2,3]&#8221; will return the list [2,3,4]. Think of this as a kind of a &#8220;foreach&#8221; loop you might know from imperative programming languages such as C,C++, Java, etc.</p>
<p>Now the &#8220;takeWhile&#8221; function. This function takes a predicate p, and a list, and returns a list such that all elements in the list meet the predicate p up to a certain point in the list, when we encounter a member that does not follow the predicate, and hence the list is trimmed.</p>
<p>For example, &#8220;takeWhile &lt;5 [1,4,2,6,3]&#8221; will return [1,4,2] since the number 6 does not follow the predicate, it is NOT less than 5.</p>
<p>So finally, what we are saying, is : &#8220;x comes from infinite list of Fibonacci numbers, but trim this list so that we only have elements less than 4 million&#8221;</p>
<p>At the very end, use the predefined &#8220;sum&#8221; function we know from the previous tutorial, to get the sum of the list.</p>
<p>Here is script executing on my machine, very rapidly thanks to that method of computing fibonacci numbers!</p>
<pre class="brush:ruby">*Euler&gt; euler2
4613732
(0.00 secs, 524700 bytes)
</pre>
<p>Thanks for reading! This is only my second tutorial, so could you please post some comments about improving my style, explanations, etc.</p>
<p>Please come back for more tutorials!</p>
]]></content:encoded>
			<wfw:commentRss>http://vlixter.com/2010/01/02/project-euler-problem-2-haskell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Project Euler :: Problem 1 :: Haskell</title>
		<link>http://vlixter.com/2010/01/02/project-euler-problem-1-haskell/</link>
		<comments>http://vlixter.com/2010/01/02/project-euler-problem-1-haskell/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 13:32:04 +0000</pubDate>
		<dc:creator>radicality</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[project-euler]]></category>

		<guid isPermaLink="false">http://vlixter.com/?p=141</guid>
		<description><![CDATA[This is the first tutorial about solving the problems on Project Euler. I will be solving problem number 1, using the language Haskell, which is a functional programming language. If you come from a imperative programming language background (C++, C, Java, PHP), you might find Haskell a little &#8216;weird&#8217; at first, but it is actually [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first tutorial about solving the problems on <a href="http://projecteuler.net/" target="_blank">Project Euler.</a></p>
<p>I will be solving problem number 1, using the language <a href="http://www.haskell.org/" target="_blank">Haskell</a>, which is a functional programming language. If you come from a imperative programming language background (C++, C, Java, PHP), you might find Haskell a little &#8216;weird&#8217; at first, but it is actually quite good for quickly solving some of the Project Euler problems!</p>
<p><strong>Problem Description:</strong></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.<br />
Find the sum of all the multiples of 3 or 5 below 1000.</p></blockquote>
<p>Solving this using Haskell is really simple. Open up ghci (Glasgow Haskell Compiler Interpreter), and then create somewhere a new file called euler1.hs, or whatever else you please.</p>
<p>Here is the code:</p>
<pre class="brush:ruby">euler1 :: Int
euler1 = sum [x | x &lt;- [1..999], x `mod` 5 == 0 || x `mod` 3 == 0]
</pre>
<p>Line 1 says that the function euler1 is of type Int (think of it as just an integer), meaning it will return an integer after calling it. Line 2 is the meat of the function. Firstly, we are using the &#8220;list comprehension&#8221; syntax. Ignore &#8220;sum&#8221; for now, and just look at what&#8217;s inbetween the square brackets. Read it as &#8220;Return me a list of numbers x, where x comes from the finite list 1 to 999, and such that x is evenly divisible by 5 OR x is evenly divisible by 3.</p>
<p>Finally, use the predefined &#8220;sum&#8221; function. This function takes a list of numeric values, and returns the sum of them, which is precisely what we want.</p>
<p>As this is my very first tutorial like this, I would appreciate any comments, such as should I write more in-depth, less in-depth, explain some concepts more, explain how to set up Haskell on your system ?</p>
<p>Thanks, and please come back for the second tutorial for Project Euler!</p>
]]></content:encoded>
			<wfw:commentRss>http://vlixter.com/2010/01/02/project-euler-problem-1-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
