<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Horia Geam&#259;n&#039;s Blog</title>
	<atom:link href="http://horiageaman.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://horiageaman.wordpress.com</link>
	<description></description>
	<lastBuildDate>Wed, 20 Apr 2011 20:10:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='horiageaman.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Horia Geam&#259;n&#039;s Blog</title>
		<link>http://horiageaman.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://horiageaman.wordpress.com/osd.xml" title="Horia Geam&#259;n&#039;s Blog" />
	<atom:link rel='hub' href='http://horiageaman.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Euler 25</title>
		<link>http://horiageaman.wordpress.com/2011/04/20/euler25/</link>
		<comments>http://horiageaman.wordpress.com/2011/04/20/euler25/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 20:10:16 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=227</guid>
		<description><![CDATA[fibs :: [Integer] fibs = 1 : 1 : zipWith (+) fibs (tail fibs) indexedFibs :: [(Int, Integer)] indexedFibs = zip [1..] fibs getFirstWithDigits :: Int -&#62; [(Int, Integer)] -&#62; Int getFirstWithDigits n (p:ps) = if snd p &#62; 10^(n-1) then fst p else getFirstWithDigits n ps answer25 = getFirstWithDigits 1000 indexedFibs<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=227&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">
fibs :: [<span style="color:#0080c0;font-style:italic;">Integer</span>]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

indexedFibs :: [(<span style="color:#0080c0;font-style:italic;">Int</span>, <span style="color:#0080c0;font-style:italic;">Integer</span>)]
indexedFibs = zip [1..] fibs

getFirstWithDigits :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; [(<span style="color:#0080c0;font-style:italic;">Int</span>, <span style="color:#0080c0;font-style:italic;">Integer</span>)] -&gt; <span style="color:#0080c0;font-style:italic;">Int</span>
getFirstWithDigits n (p:ps) = <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> snd p &gt; 10^(n-1) <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> fst p <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> getFirstWithDigits n ps

answer25 = getFirstWithDigits 1000 indexedFibs
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=227&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2011/04/20/euler25/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Euler 24</title>
		<link>http://horiageaman.wordpress.com/2011/04/20/euler24/</link>
		<comments>http://horiageaman.wordpress.com/2011/04/20/euler24/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 20:00:30 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=222</guid>
		<description><![CDATA[import Data.List (sort) -- A string used just in examples: permutation' = "03142" -- Create a function, nextPermutation, that generates the next lexicographic permutation. type Index = Int -- 0] First, a helper: indexed :: (Ord a) =&#62; [a] -&#62; [(a, Index)] indexed xs = zip xs [0 .. length xs - 1] -- 1] [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=222&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">
<span style="color:#3333ff;font-style:normal;font-weight:bold;">import</span> Data.List (sort)

<span style="color:#006600;font-style:italic;">-- A string used just in examples:</span>
permutation' = "03142"

<span style="color:#006600;font-style:italic;">-- Create a function, nextPermutation, that generates the next lexicographic permutation.</span>

<span style="color:#3333ff;font-style:normal;font-weight:bold;">type</span> <span style="color:#0080c0;font-style:italic;">Index</span> = <span style="color:#0080c0;font-style:italic;">Int</span>

<span style="color:#006600;font-style:italic;">-- 0] First, a helper:</span>
indexed :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [a] -&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)]
indexed xs = zip xs [0 .. length xs - 1]

<span style="color:#006600;font-style:italic;">-- 1] Looking in reverse order, find the first item that is smaller than its successor. We call it, "pivot".</span>
<span style="color:#006600;font-style:italic;">--    Note: An empty list as pivot is a signal that no new permutation can be generated.</span>
firstDefect :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)] -&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)]
firstDefect []          = []
firstDefect (xi:[])     = []
firstDefect (xi:yi:zis) = <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> fst yi &lt; fst xi <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> [(fst yi, snd yi)] <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> firstDefect (yi:zis)

findPivot :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)] -&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)]
findPivot = firstDefect . reverse

<span style="color:#006600;font-style:italic;">-- For example:</span>
pivot' = findPivot $ indexed permutation'

<span style="color:#006600;font-style:italic;">-- 2] Find the smallest value in the sublist that follows the pivot that is still greater than the pivot. We call it, "new pivot".</span>
newPivotCandidates :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)] -&gt; (a, <span style="color:#0080c0;font-style:italic;">Index</span>) -&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)]
newPivotCandidates xis (pivot, pivotIdx) = [zi| zi &lt;- xis, fst zi &gt; pivot &amp;&amp; snd zi &gt; pivotIdx ]

findSmallestCandidate :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)] -&gt; [(a, <span style="color:#0080c0;font-style:italic;">Index</span>)]
findSmallestCandidate []         = []
findSmallestCandidate (p:[])     = [p]
findSmallestCandidate (p1:p2:ps) = <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> (fst p1) &lt; (fst p2) <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> findSmallestCandidate (p1:ps) <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> findSmallestCandidate (p2:ps)

<span style="color:#006600;font-style:italic;">-- For example</span>
newPivot' = findSmallestCandidate $ newPivotCandidates (indexed permutation') (head pivot')

<span style="color:#006600;font-style:italic;">-- 3] Then, the algorithm to generate the next permutaton is this:</span>
<span style="color:#006600;font-style:italic;">--  3.1] Copy everything from the original up to before the pivot;</span>
<span style="color:#006600;font-style:italic;">--  3.2] Then append the new pivot;</span>
<span style="color:#006600;font-style:italic;">--  3.3] Then, in the sequence that folowed the position of the pivot replace the new pivot with pivot;</span>
<span style="color:#006600;font-style:italic;">--  3.4] Then reorder the sequence generated in step 3.3] ascendingly and append it to the newly rebuilt list.</span>
<span style="color:#006600;font-style:italic;">--    For example, next("03142') is "03214"</span>

replace :: (<span style="color:#0080c0;font-style:italic;">Eq</span> a) =&gt; a -&gt; a -&gt; [a] -&gt; [a]
replace a a' [] = []
replace a a' (x:xs)
    | x == a    = a' : (replace a a' xs)
	| otherwise = x  : (replace a a' xs)

nextPermutation :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [a] -&gt; [a]
nextPermutation permutation =
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">let</span> pivot = findPivot $ indexed permutation
        pivotValue = fst (head pivot)
        pivotIndex = snd (head pivot)
        newPivot = findSmallestCandidate $ newPivotCandidates (indexed permutation) (head pivot)
        newPivotValue = fst (head newPivot)
        newPivotIndex = snd (head newPivot)
        step3_1 = take pivotIndex permutation
        step3_2 = [newPivotValue]
        step3_3 = sort (replace newPivotValue pivotValue (drop (pivotIndex + 1) permutation))
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">in</span> step3_1 ++ step3_2 ++ step3_3

<span style="color:#006600;font-style:italic;">-- Then solve the problem:</span>
permute :: (<span style="color:#0080c0;font-style:italic;">Ord</span> a) =&gt; [a] -&gt; <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; [a]
permute !xs n
    | n == 0    = xs
    | otherwise = permute (nextPermutation xs) (n-1)
<span style="color:#006600;font-style:italic;">-- (to use the bang, :set -XBangPatterns in GHC)</span>

answer24 = permute "01234567890" 999999
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/222/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=222&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2011/04/20/euler24/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 23</title>
		<link>http://horiageaman.wordpress.com/2011/01/04/euler23/</link>
		<comments>http://horiageaman.wordpress.com/2011/01/04/euler23/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 20:36:58 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=218</guid>
		<description><![CDATA[This is pretty slow (15 min.). The main hog is the function madeOfAbundants; when I have time, I need to look for a faster algorithm. halfDivisors :: Integer -&#62; [Integer] halfDivisors n = [ d &#124; d &#60;- [2..squareRoot], n `mod` d == 0 ] where squareRoot = floor $ sqrt $ fromIntegral n properDivisors [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=218&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is pretty slow (15 min.). The main hog is the function <strong>madeOfAbundants</strong>; when I have time, I need to look for a faster algorithm.</p>
<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">
halfDivisors :: <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; [<span style="color:#0080c0;font-style:italic;">Integer</span>]
halfDivisors n = [ d | d &lt;- [2..squareRoot], n `mod` d == 0 ] <span style="color:#3333ff;font-style:normal;font-weight:bold;">where</span> squareRoot = floor $ sqrt $ fromIntegral n

properDivisors :: <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; [<span style="color:#0080c0;font-style:italic;">Integer</span>]
properDivisors n = [1] ++ halfDivs ++ [d'| d &lt;- halfDivs, let d' = n `div` d, d' &gt; d] <span style="color:#3333ff;font-style:normal;font-weight:bold;">where</span> halfDivs = halfDivisors n

isAbundant :: <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; <span style="color:#0080c0;font-style:italic;">Bool</span>
isAbundant n = (sum $ properDivisors n) &gt; n

abundantsUpTo :: <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; [<span style="color:#0080c0;font-style:italic;">Integer</span>]
abundantsUpTo n = filter isAbundant [1..n]

abundants :: [<span style="color:#0080c0;font-style:italic;">Integer</span>]
abundants = abundantsUpTo 28123

madeOfAbundants :: <span style="color:#0080c0;font-style:italic;">Integer</span> -&gt; <span style="color:#0080c0;font-style:italic;">Bool</span>
madeOfAbundants n = not $ null [a1 | a1 &lt;- filter (&lt;= (n `div` 2)) abundants, elem (n - a1) abundants]

answer23 = sum [n | n &lt;- [1..28123], not (madeOfAbundants n)]</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/218/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=218&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2011/01/04/euler23/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 22</title>
		<link>http://horiageaman.wordpress.com/2010/05/10/euler22/</link>
		<comments>http://horiageaman.wordpress.com/2010/05/10/euler22/#comments</comments>
		<pubDate>Mon, 10 May 2010 18:04:25 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=188</guid>
		<description><![CDATA[import System.IO import List import Char {- This function transforms a string of the form ""MARY","PATRICIA","LINDA","BARBARA"" (which is what we get after reading the names file) into the string "MARY PATRICIA LINDA BARBARA"; that is, it removes the inner double quotes and replaces the commas with spaces, thus preparing the string to be split into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=188&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">
<span style="color:#3333ff;font-style:normal;font-weight:bold;">import</span> System.IO
<span style="color:#3333ff;font-style:normal;font-weight:bold;">import</span> List
<span style="color:#3333ff;font-style:normal;font-weight:bold;">import</span> Char

<span style="color:#006600;font-style:italic;">{-
This function transforms a string of the form ""MARY","PATRICIA","LINDA","BARBARA"" (which is
what we get after reading the names file) into the string "MARY PATRICIA LINDA BARBARA";
that is, it removes the inner double quotes and replaces the commas with spaces, thus preparing
the string to be split into words using the 'words' library function.
-}</span>
toWords :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">String</span>
toWords []      = []
toWords (c:cs)
    | c == '\"' =       toWords cs
    | c == ','  = ' ' : toWords cs
    | otherwise = c   : toWords cs

<span style="color:#006600;font-style:italic;">--Gets the individual names from the contents of the file and sorts them alphabetically:</span>
getNames :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; [<span style="color:#0080c0;font-style:italic;">String</span>]
getNames = sort . words . toWords

<span style="color:#006600;font-style:italic;">--Pairs each name with its index:</span>
indexedNames :: [<span style="color:#0080c0;font-style:italic;">String</span>] -&gt; [(<span style="color:#0080c0;font-style:italic;">Integer</span>, <span style="color:#0080c0;font-style:italic;">String</span>)]
indexedNames ns = zip [1 .. toInteger $ length ns] ns

<span style="color:#006600;font-style:italic;">--Calculates the score of a name, irrespective of its index:</span>
nameScore :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">Integer</span>
nameScore [] = 0
nameScore (c:cs) = toInteger ((ord c) - (ord 'A') + 1) + nameScore cs

<span style="color:#006600;font-style:italic;">--Calculates the score of a name, taking its index into consideration:</span>
indexedScore :: (<span style="color:#0080c0;font-style:italic;">Integer</span>, <span style="color:#0080c0;font-style:italic;">String</span>) -&gt; <span style="color:#0080c0;font-style:italic;">Integer</span>
indexedScore (i, s) = i * nameScore s

<span style="color:#006600;font-style:italic;">--Calculates the sum of all scores:</span>
solve22 :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">Integer</span>
solve22 contents = sum [indexedScore n | n &lt;- indexedNames $ getNames contents]

main = <span style="color:#3333ff;font-style:normal;font-weight:bold;">do</span>
    withFile "names.txt" <span style="color:#0080c0;font-style:italic;">ReadMode</span> (\handle -&gt; <span style="color:#3333ff;font-style:normal;font-weight:bold;">do</span>
        contents &lt;- hGetContents handle
        <span style="color:#3333ff;font-style:normal;font-weight:bold;">let</span> answer22 = solve22 contents
        putStr $ show answer22)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/188/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=188&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/05/10/euler22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 21</title>
		<link>http://horiageaman.wordpress.com/2010/03/10/euler21/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/10/euler21/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 13:58:33 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=141</guid>
		<description><![CDATA[properDivisors :: Int -&#62; [Int] properDivisors n = [ x &#124; x &#60;- [1..n `div` 2], n `mod` x == 0] properSum :: Int -&#62; Int properSum = sum . properDivisors isAmicable :: Int -&#62; Bool isAmicable n = n == properSum n' &#38;&#38; n /= n' where n' = properSum n amicables :: [Int] [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=141&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">properDivisors :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; [<span style="color:#0080c0;font-style:italic;">Int</span>]
properDivisors n = [ x | x &lt;- [1..n `div` 2], n `mod` x == 0]

properSum :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; <span style="color:#0080c0;font-style:italic;">Int</span>
properSum  = sum . properDivisors

isAmicable :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; <span style="color:#0080c0;font-style:italic;">Bool</span>
isAmicable n = n == properSum n' &amp;&amp; n /= n' <span style="color:#3333ff;font-style:normal;font-weight:bold;">where</span> n' = properSum n

amicables :: [<span style="color:#0080c0;font-style:italic;">Int</span>]
amicables = filter isAmicable [1..10000]

answer21 = sum amicables</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=141&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/10/euler21/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 20</title>
		<link>http://horiageaman.wordpress.com/2010/03/10/euler20/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/10/euler20/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 13:55:13 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=137</guid>
		<description><![CDATA[answer20 = sum [read [c] &#124; c &#60;- show $ product [1..100]]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=137&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">answer20 = sum [read [c] | c &lt;- show $ product [1..100]]</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/137/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=137&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/10/euler20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 19</title>
		<link>http://horiageaman.wordpress.com/2010/03/09/euler19/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/09/euler19/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 18:55:12 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=131</guid>
		<description><![CDATA[A little too slow (just over 100s), but an opportunity to write a few useful helper functions. type Date = (Int, Int, Int) -- Gets the next day relative to the given date: nextDay :: Date -&#62; Date nextDay (y, m, d) &#124; d == 28 &#38;&#38; m == 2 &#38;&#38; (y `mod` 4 /= [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=131&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A little too slow (just over 100s), but an opportunity to write a few useful helper functions.</p>
<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;"><span style="color:#3333ff;font-style:normal;font-weight:bold;">type</span> <span style="color:#0080c0;font-style:italic;">Date</span> = (<span style="color:#0080c0;font-style:italic;">Int</span>, <span style="color:#0080c0;font-style:italic;">Int</span>, <span style="color:#0080c0;font-style:italic;">Int</span>)

<span style="color:#006600;font-style:italic;">-- Gets the next day relative to the given date:</span>
nextDay :: <span style="color:#0080c0;font-style:italic;">Date</span> -&gt; <span style="color:#0080c0;font-style:italic;">Date</span>
nextDay (y, m, d)
    | d == 28 &amp;&amp; m == 2 &amp;&amp; (y `mod` 4 /= 0 || y == 1900) = (y, 3, 1)
    | d == 29 &amp;&amp; m == 2                                  = (y, 3, 1)
    | d == 30 &amp;&amp; elem m [4, 6, 9, 11]                    = (y, m+1, 1)
    | d == 31                                            = <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> m == 12 <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> (y+1, 1, 1) <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> (y, m+1, 1)
    | otherwise                                          = (y, m, d+1) 

<span style="color:#006600;font-style:italic;">-- All the dates since 1900-01-01:</span>
sinceJan1st1900 :: [<span style="color:#0080c0;font-style:italic;">Date</span>]
sinceJan1st1900 = (1900, 1, 1) : map (nextDay) sinceJan1st1900

<span style="color:#006600;font-style:italic;">-- Is the first date before the second date?</span>
before :: <span style="color:#0080c0;font-style:italic;">Date</span> -&gt; <span style="color:#0080c0;font-style:italic;">Date</span> -&gt; <span style="color:#0080c0;font-style:italic;">Bool</span>
before (y1, m1, d1) (y2, m2, d2)
    | y1 &gt; y2                          = <span style="color:#0080c0;font-style:italic;">False</span>
    | y1 == y2 &amp;&amp; m1 &gt; m2              = <span style="color:#0080c0;font-style:italic;">False</span>
    | y1 == y2 &amp;&amp; m1 == m2 &amp;&amp; d1 &gt;= d2 = <span style="color:#0080c0;font-style:italic;">False</span>
    | otherwise                        = <span style="color:#0080c0;font-style:italic;">True</span>	

<span style="color:#006600;font-style:italic;">-- All the dates between 1900-01-01 and 2000-12-31:</span>
xxCentury :: [<span style="color:#0080c0;font-style:italic;">Date</span>]
xxCentury = takeWhile (`before` (2001, 1, 1)) sinceJan1st1900

<span style="color:#006600;font-style:italic;">-- Gets the day before the given date:</span>
prevDay :: <span style="color:#0080c0;font-style:italic;">Date</span> -&gt; <span style="color:#0080c0;font-style:italic;">Date</span>
prevDay (y, m, d)
    | d &gt; 1     = (y, m, d-1)
    | otherwise =
        <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> m == 1
            <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> (y-1, 12, 31)
            <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span>
                <span style="color:#3333ff;font-style:normal;font-weight:bold;">case</span> m <span style="color:#3333ff;font-style:normal;font-weight:bold;">of</span>
                    2 -&gt; (y, 1, 31)
                    3 -&gt; <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> (y `mod` 4 == 0) &amp;&amp; (y /= 1900) <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> (y, 2, 29) <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> (y, 2, 28)
                    4 -&gt; (y, 3, 31)
                    5 -&gt; (y, 4, 30)
                    6 -&gt; (y, 5, 31)
                    7 -&gt; (y, 6, 30)
                    8 -&gt; (y, 7, 31)
                    9 -&gt; (y, 8, 31)
                    10 -&gt; (y, 9, 30)
                    11 -&gt; (y, 10, 31)
                    12 -&gt; (y, 11, 30)

<span style="color:#006600;font-style:italic;">-- Day index relative to Jan 1st, 1900:</span>
dayIndex :: <span style="color:#0080c0;font-style:italic;">Date</span> -&gt; <span style="color:#0080c0;font-style:italic;">Int</span>
dayIndex (1900, 1, 1) = 0
dayIndex d            = 1 + dayIndex (prevDay d)

<span style="color:#006600;font-style:italic;">-- Get all the dates like YYYY-MM-01 between 1901-01-01 and 2000-12-31 together with their respective indices :</span>
firstsOfMonth :: [(<span style="color:#0080c0;font-style:italic;">Date</span>, <span style="color:#0080c0;font-style:italic;">Int</span>)]
firstsOfMonth = [((y, m, d), dayIndex (y, m, d)) | (y, m, d) = 1901]

<span style="color:#006600;font-style:italic;">-- Since 1900-01-01 was a Monday and its index is 0, the Sundays are all firstsOfMonths whose indices are 7×k + 6:</span>
sundays :: [<span style="color:#0080c0;font-style:italic;">Date</span>]
sundays = [d | (d, i) &lt;- firstsOfMonth, i `mod` 7 == 6]

answer19 = length sundays</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/131/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=131&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/09/euler19/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 18</title>
		<link>http://horiageaman.wordpress.com/2010/03/08/euler18/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/08/euler18/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 02:59:25 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=125</guid>
		<description><![CDATA[So, there is a mysterious route through the triangle that gives the maximum sum. We are not required to find that route and we don&#8217;t care about it, but we know that once we are in a node on the last but one layer, we will go to the more valuable of its two descendants. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=125&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So, there is a mysterious route through the triangle that gives the maximum sum. We are not required to find that route and we don&#8217;t care about it, but we know that once we are in a node on the last but one layer, we will go to the more valuable of its two descendants. That means that we can collapse any node of the last but one layer with the sum of its own value and the value of its most valuable descendant.</p>
<p>Therefore, an algorithm to solve the problem could be:</p>
<p>Replace the value in each of the nodes in the last but one row with the sum of the value in the node and the value in its most valuable descendant. Then get rid of the last row. Repeat until the tree is reduced to the root node. The value of the root is the answer.</p>
<p>For example, we replace the value in the first node of the last but one row (that is, 63) with the maximum between 63+4 and 63+62. Then, the 2nd node of the last but one row (66) is replaced with max (66+62, 66+98) and so on. At the end of the first iteration we get rid of the original last row, and the new last row has the nodes [125, 164, 102, ..., 54].</p>
<p><em>Note:</em> Since we start the processing with the last row, in the actual implementation we first reverse the triangle so we work on the widest row and then call the algorithm recursively for the remaining rows below until we get to the root.</p>
<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">triangle :: [[<span style="color:#0080c0;font-style:italic;">Int</span>]]
triangle = [
            [75],
            [95, 64],
            [17, 47, 82],
            [18, 35, 87, 10],
            [20, 04, 82, 47, 65],
            [19, 01, 23, 75, 03, 34],
            [88, 02, 77, 73, 07, 63, 67],
            [99, 65, 04, 28, 06, 16, 70, 92],
            [41, 41, 26, 56, 83, 40, 80, 70, 33],
            [41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
            [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
            [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
            [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
            [63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
            [04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23]
           ]

reversed :: [[<span style="color:#0080c0;font-style:italic;">Int</span>]]
reversed = reverse triangle

transform :: [[<span style="color:#0080c0;font-style:italic;">Int</span>]] -&gt; [[<span style="color:#0080c0;font-style:italic;">Int</span>]]
transform [r] = [r]
transform (zs:ys:xss) = transform ([max ((ys!!i) + (zs!!i)) ((ys!!i) + (zs!!(i+1))) | i &lt;- [0..(length zs - 2)]] : xss)

answer18 = head (head (transform reversed))</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/125/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=125&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/08/euler18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 17</title>
		<link>http://horiageaman.wordpress.com/2010/03/08/euler17/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/08/euler17/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 21:05:22 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=117</guid>
		<description><![CDATA[While at this, why just determine the number of characters and not write a function that generates the numeral for any number between 1 to 1000? The function numeral does exactly that: unitName :: String -&#62; String unitName s = case read s of 1 -&#62; "one" 2 -&#62; "two" 3 -&#62; "three" 4 -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=117&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While at this, why just determine the number of characters and not write a function that generates the <a href="http://en.wikipedia.org/wiki/English_numerals">numeral</a> for any number between 1 to 1000? The function <em>numeral</em> does exactly that:</p>
<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">unitName :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">String</span>
unitName s =
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">case</span> read s <span style="color:#3333ff;font-style:normal;font-weight:bold;">of</span>
        1 -&gt; "one"
        2 -&gt; "two"
        3 -&gt; "three"
        4 -&gt; "four"
        5 -&gt; "five"
        6 -&gt; "six"
        7 -&gt; "seven"
        8 -&gt; "eight"
        9 -&gt; "nine"
        _ -&gt; ""

teenName :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">String</span>
teenName s =
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">case</span> read s <span style="color:#3333ff;font-style:normal;font-weight:bold;">of</span>
        11 -&gt; "eleven"
        12 -&gt; "twelve"
        13 -&gt; "thirteen"
        14 -&gt; "fourteen"
        15 -&gt; "fifteen"
        16 -&gt; "sixteen"
        17 -&gt; "seventeen"
        18 -&gt; "eighteen"
        19 -&gt; "nineteen"
        _  -&gt; "ten"

tenName :: <span style="color:#0080c0;font-style:italic;">String</span> -&gt; <span style="color:#0080c0;font-style:italic;">String</span>
tenName s =
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">case</span> s <span style="color:#3333ff;font-style:normal;font-weight:bold;">of</span>
        "2" -&gt; "twenty"
        "3" -&gt; "thirty"
        "4" -&gt; "forty"
        "5" -&gt; "fifty"
        "6" -&gt; "sixty"
        "7" -&gt; "seventy"
        "8" -&gt; "eighty"
        "9" -&gt; "ninety"
        _   -&gt; ""

numeral :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; <span style="color:#0080c0;font-style:italic;">String</span>
numeral n =
    <span style="color:#3333ff;font-style:normal;font-weight:bold;">case</span> length s <span style="color:#3333ff;font-style:normal;font-weight:bold;">of</span>
        1 -&gt; unitName s
        2 -&gt; <span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> head s == '1' <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> teenName s <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> (tenName ((head s):[])) ++ (numeral (read (tail s)))
        3 -&gt; (unitName ((head s):[])) ++ " hundred" ++ (<span style="color:#3333ff;font-style:normal;font-weight:bold;">if</span> read (tail s) == 0 <span style="color:#3333ff;font-style:normal;font-weight:bold;">then</span> "" <span style="color:#3333ff;font-style:normal;font-weight:bold;">else</span> " and " ++ (numeral (read (tail s))))
        _ -&gt; "one thousand"
<span style="color:#3333ff;font-style:normal;font-weight:bold;">    </span><span style="color:#3333ff;font-style:normal;font-weight:bold;">where</span> s = show n		

numerals :: <span style="color:#0080c0;font-style:italic;">Int</span> -&gt; [<span style="color:#0080c0;font-style:italic;">String</span>]
numerals x = [numeral n | n &lt;- [1..x]]

allLetters :: <span style="color:#0080c0;font-style:italic;">String</span>
allLetters = concat (numerals 1000)

answer17 = length [c | c &lt;- allLetters, 'a' &lt;= c &amp;&amp; c &lt;= 'z' ]</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=117&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/08/euler17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 16</title>
		<link>http://horiageaman.wordpress.com/2010/03/06/euler16/</link>
		<comments>http://horiageaman.wordpress.com/2010/03/06/euler16/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 15:23:56 +0000</pubDate>
		<dc:creator>horiageaman</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://horiageaman.wordpress.com/?p=113</guid>
		<description><![CDATA[answer16 = sum [read (c:[]) &#124; c &#60;- show (2^1000)]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=113&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre style="line-height:14px;background-color:#eee;width:100%;overflow:auto;border:#999999 1px dashed;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;color:#000000;padding:5px;">answer16 = sum [read (c:[]) | c &lt;- show (2^1000)]</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/horiageaman.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/horiageaman.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/horiageaman.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=horiageaman.wordpress.com&amp;blog=8521038&amp;post=113&amp;subd=horiageaman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://horiageaman.wordpress.com/2010/03/06/euler16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">horiageaman</media:title>
		</media:content>
	</item>
	</channel>
</rss>
