<?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>A broader view</title>
	<atom:link href="http://breadthfirst.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://breadthfirst.wordpress.com</link>
	<description>A breadth-first search through the arts &#38; sciences</description>
	<lastBuildDate>Mon, 19 Dec 2011 19:17:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='breadthfirst.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>A broader view</title>
		<link>http://breadthfirst.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://breadthfirst.wordpress.com/osd.xml" title="A broader view" />
	<atom:link rel='hub' href='http://breadthfirst.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Getting Started with LLVM and the Ocaml LLVM Tutorial</title>
		<link>http://breadthfirst.wordpress.com/2011/01/14/getting-started-llvm-ocaml-tutorial/</link>
		<comments>http://breadthfirst.wordpress.com/2011/01/14/getting-started-llvm-ocaml-tutorial/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 03:38:07 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=729</guid>
		<description><![CDATA[I recently started playing with LLVM, and I ran into a few issues with getting started that didn’t have google-able solutions, so I’m posting this on the off chance it might help someone else. The directions on the LLVM website are very detailed, but intimidatingly long. Getting a build working is actually really simple. Aaron [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=729&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently started playing with LLVM, and I ran into a few issues with getting started that didn’t have google-able solutions, so I’m posting this on the off chance it might help someone else.</p>
<p>The directions on the <a href="http://llvm.org/docs/GettingStarted.html">LLVM website</a> are very detailed, but intimidatingly long. Getting a build working is actually really simple. Aaron Gray put together an amazingly easy four step guide on bulding LLVM for cygwin&nbsp;<a href="http://www.aarongray.org/LLVM/BuildingLLVMonCygwinANG.html">here</a> (and the directions worked fine for me on Unbuntu, too). It looks like he was working with an older version of LLVM, and you can actually skip his “build the tools” step. If you want to do that step anyway, and follow his instructions with the current version of cygwin, the tools will drop into a “Debug+Asserts” directory, instead of a “debug” directory.</p>
<p>Also, on cygwin, you can build with gcc3 or gcc4 but, if you have gcc3 and ocaml installed, you have to install gcc4 (or fiddle with your path). Ocaml installs enough components of gcc4 that configure detects that you have gcc4, but during the compile process, cc1plus from gcc3 will complain about arguments that are only legal with gcc4:</p>
<p>cc1plus: error: unrecognized command line option &#8220;-Wno-variadic-macros&#8221;<br />
make[3]: *** [llvm-main.o] Error 1</p>
<p>If you’re on 64-bit Ubuntu, you’ll need to do sudo apt-get install libc6-dev-i386, or you’ll get an error related to stubs-32.h</p>
<p>That’s it! It’s really that simple.</p>
<p>If you want to build a back-end, there’s a nice guide <a href="http://llvm.org/docs/WritingAnLLVMBackend.html">here</a>. As with the official instructions, the prerequisite reading list, which states “These essential documents must be read before reading this document”, is intimidating long, but it’s actually simple enough that you can just dive in and refer to the detailed manual when an obscure reference pops up, JIT :-).</p>
<p>There’s an even simpler guide to writing a front-end by Chris Lattner <a href="http://llvm.org/docs/tutorial/index.html">here</a>, with an Ocaml translation by Erick Tryzelaar <a href="http://llvm.org/docs/tutorial/OCamlLangImpl1.html">here</a>.</p>
<p>The Ocaml tutorial just requires LLVM + Ocaml + camlp4. If you don’t have camlp4, you’ll know it because you’ll get</p>
<p>/usr/bin/ocamldep -pp camlp4of -modules lexer.ml &gt; lexer.ml.depends<br />
sh: camlp4of: not found</p>
<p>On Ubuntu, you just need to do sudo apt-get install camlp4-extra to fix that.</p>
<p>Also, the tutorial has a few minor bugs.</p>
<p>In chapter 3</p>
<p><pre class="brush: fsharp;">
| '+' -&gt; build_add lhs_val rhs_val &quot;addtmp&quot; builder
| '-' -&gt; build_sub lhs_val rhs_val &quot;subtmp&quot; builder
| '*' -&gt; build_mul lhs_val rhs_val &quot;multmp&quot; builder
</pre></p>
<p>Should be</p>
<p><pre class="brush: fsharp;">
| '+' -&gt; build_fadd lhs_val rhs_val &quot;addtmp&quot; builder
| '-' -&gt; build_fsub lhs_val rhs_val &quot;subtmp&quot; builder
| '*' -&gt; build_fmul lhs_val rhs_val &quot;multmp&quot; builder
</pre></p>
<p>Since everything is converted to a double, but the base build_* functions expect an int, you&#8217;ll get</p>
<p>lvm::BinaryOperator::init(llvm::Instruction::BinaryOps): Assertion `getType()-&gt;isIntOrIntVectorTy() &amp;&amp; &#8220;Tried to create an integer operation on a non-integer type!&#8221;&#8216; failed.</p>
<p>unless you use operations that expect floats</p>
<p>Similarly, in Chapter 5,</p>
<p><pre class="brush: fsharp;">
let next_var = build_add variable step_val &quot;nextvar&quot; builder in
</pre></p>
<p>should be</p>
<p><pre class="brush: fsharp;">
let next_var = build_fadd variable step_val &quot;nextvar&quot; builder in
</pre></p>
<p>Also, in chapter 4, to get extern functions working, see <a href="http://old.nabble.com/Linking-with-C-Library-td28133460.html">this</a>.</p>
<p>And if you compiled using the instructions above, you&#8217;ll need to build with ocamlbuild -cflags -I,+llvm-2.8 -lflags -I,+llvm-2.8 toy.byte instead of just ocamlbuild toy.byte (or you can modify your build file).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/729/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/729/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=729&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2011/01/14/getting-started-llvm-ocaml-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Mixed-language designs with Xilinx tools: be careful</title>
		<link>http://breadthfirst.wordpress.com/2010/10/27/mixed-language-xilinx/</link>
		<comments>http://breadthfirst.wordpress.com/2010/10/27/mixed-language-xilinx/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 21:37:09 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=722</guid>
		<description><![CDATA[I had a couple of bugs that each took me a few days to track down, because it just didn’t occur to me that the compiler might just be wrong. Googling around for related issues didn’t help me, but perhaps this will save someone else some time. Using the version of Xst package with ISE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=722&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a couple of bugs that each took me a few days to track down, because it just didn’t occur to me that the compiler might just be wrong. Googling around for related issues didn’t help me, but perhaps this will save someone else some time.</p>
<p>Using the version of Xst package with ISE 10.x, Verilog modules instantiated in VHDL files can get incorrectly optimized away. Moving to 12.x solved that issue. It wouldn’t have even occurred to me to look for that had a co-worker not mentioned that he had problems with some of his code getting optimized away in 9.X, when he was creating a multi-banked RAM (without mixed-language code) – the synthesis tool decided that one bank just wasn’t necessary, and removed it entirely. Moving backwards to version 8.x fixed his issue.</p>
<p>Even in the latest version, there are still mixed language issues. A VHDL IOBUF connected to a Verilog module may get Xs on the output of the IOBUF in ISim even if T, the IOBUF select, is high, and the output of the IOBUF is only connected to Verilog module inputs. Switching to a Verilog wrapper and instantiating the IOBUF in Verilog fixed that problem.  I never would have thought to try swapping out a VHDL instantiation for an identical Verilog instantiation had I not run into a mixed language previously.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/722/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/722/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/722/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=722&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/10/27/mixed-language-xilinx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 219</title>
		<link>http://breadthfirst.wordpress.com/2010/07/10/project-euler-problem-219/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/10/project-euler-problem-219/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 16:37:07 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=712</guid>
		<description><![CDATA[Skew-cost coding Let A and B be bit strings (sequences of 0&#8242;s and 1&#8242;s). If A is equal to the leftmost length(A) bits of B, then A is said to be a prefix of B. For example, 00110 is a prefix of 001101001, but not of 00111 or 100110. A prefix-free code of size n is a collection of n distinct bit strings such that no string is a prefix of any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=712&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Skew-cost coding</strong></p>
<p>Let A and B be bit strings (sequences of 0&#8242;s and 1&#8242;s).<br />
If A is equal to the leftmost length(A) bits of B, then A is said to be a prefix of B.<br />
For example, 00110 is a prefix of 001101001, but not of 00111 or 100110.</p>
<p>A prefix-free code of size <var>n</var> is a collection of <var>n</var> distinct bit strings such that no string is a prefix of any other. For example, this is a prefix-free code of size 6:</p>
<p>0000, 0001, 001, 01, 10, 11</p>
<p>Now suppose that it costs one penny to transmit a &#8217;0&#8242; bit, but four pence to transmit a &#8217;1&#8242;.<br />
Then the total cost of the prefix-free code shown above is 35 pence, which happens to be the cheapest possible for the skewed pricing scheme in question.<br />
In short, we write Cost(6) = 35.</p>
<p>What is Cost(10<sup>9</sup>) ?</p></blockquote>
<p>A lot of people stumbled across this blog, searching for the solution to problem 219, which seemed like a good indication that it might be a fun problem, so I skipped ahead to try it out. Unlike the early problems that I&#8217;d been solving, the really brain-dead obvious solution didn&#8217;t work here: using Huffman coding and maintaining the entire tree is way too big. But, since we always take the lowest cost node, and add 1 and 4 to the costs, the range of node costs at any time is at most five, so the slightly less brain-dead solution of keeping track of node counts and iterating works fine.</p>
<p><pre class="brush: fsharp;">
module Problem219 =
    let cost (c,(n0,n1,n2,n3,n4)) = c*n0 + (c+1L)*(n1) + (c+2L)*(n2) + (c+3L)*(n3) + (c+4L)*(n4)

    let counts = (1L,(1L,0L,0L,1L,0L)) |&gt; Seq.unfold(fun (c,(n0,n1,n2,n3,n4)) -&gt;
        if n0 &gt; 0L then Some((c,(n0,n1,n2,n3,n4)), (c,(n0-1L,n1+1L,n2,n3,n4+1L)))
        else Some((c,(n0,n1,n2,n3,n4)), (c+1L,(n1-1L,n2+1L,n3,n4,1L))))

    let ans = cost (Seq.nth (1000000000-2) counts)
</pre></p>
<p>In retrospect, using a recursive function instead of Seq.unfold probably would have been cleaner and, of course, this could be done in log time by doing n0 steps in single iteration, but I&#8217;m trying to keep my total time spent solving problems under my time spent writing up solution, so I&#8217;ll stick with this solution. I&#8217;ll probably go back and re-write the cost function at some point, though, since it&#8217;s just plain ugly.</p>
<p>A conversation with <a href="http://sethholloway.com/blog/">Seth</a> the other day reminded me that I should be unit testing, so I started playing with xUnit, putting the incremental tests I&#8217;d been doing in the REPL into unit tests.</p>
<p><pre class="brush: fsharp;">
    [&lt;Fact&gt;]
    let Test1 = Seq.nth 3 counts|&gt; MustEqual (3L, (0L, 2L, 1L, 1L, 1L))
    [&lt;Fact&gt;]
    let Test2 = Seq.nth 5 counts|&gt; MustEqual (4L, (0L, 3L, 1L, 1L, 2L))
    [&lt;Fact&gt;]
    let Test3 = minCost 6 |&gt; MustEqual 35L
    [&lt;Fact&gt;]
    let Test4 = minCost 12 |&gt; MustEqual 96L
</pre></p>
<p>I&#8217;m probably going to cut back on project Euler for now; I started working through these as a way to scratch my coding itch and learn F#, since I hadn&#8217;t written any code for about a year, but the problems aren&#8217;t very coding intensive, and I&#8217;ve started writing some F# and JavaScript for my <a href="http://www.linkedin.com/in/danluu">day job</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/712/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=712&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/10/project-euler-problem-219/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 21</title>
		<link>http://breadthfirst.wordpress.com/2010/07/07/project-euler-problem-21/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/07/project-euler-problem-21/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 07:45:57 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=690</guid>
		<description><![CDATA[Evaluate the sum of all the amicable numbers under 10000. The naive solution I came up for this returns a sequence of pairs. I&#8217;m not sure what the idiomatic and/or efficient solution is in F#, so I just yield!ed the pair as a list. Any F# experts have a better way? I should add a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=690&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>Evaluate the sum of all the amicable numbers under 10000.</p></blockquote>
<p><pre class="brush: fsharp;">
    let sumOfDivisors n = seq {for m in 2..int(sqrt(float n)) do if n % m = 0 then yield! [m;n/m]} |&gt; Seq.sum |&gt; (+) 1
    let ans = {1..10000} |&gt; Seq.filter (fun n -&gt; sumOfDivisors (sumOfDivisors n) = n &amp;&amp; sumOfDivisors n &lt;&gt; n) |&gt; Seq.sum
</pre></p>
<p>The naive solution I came up for this returns a sequence of pairs. I&#8217;m not sure what the idiomatic and/or efficient solution is in F#, so I just yield!ed the pair as a list. Any F# experts have a better way?</p>
<p>I should add a disclaimer that while this sum of divisors function works for this problem,  it&#8217;s actually wrong for perfect squares, just in case anyone stumbles on this code and tries to use it.</p>
<p>After seeing how explicitly calling DivRem sped up <a href="http://breadthfirst.wordpress.com/2010/06/30/project-euler-problem-16/">problem 16</a>, I wondered if explicitly saving the sum would speed this up.</p>
<p><pre class="brush: fsharp;">
    let sumOfDivisors n = seq {for m in 2..int(sqrt(float n)) do if n % m = 0 then yield! [m;n/m]} |&gt; Seq.sum |&gt; (+) 1
    let amicable n =
        let m = sumOfDivisors n
        n = sumOfDivisors m &amp;&amp; m &lt;&gt; n
    let ans = {1..10000} |&gt; Seq.filter amicable |&gt; Seq.sum
</pre></p>
<p>But this gives the exact same performance; I guess the compiler does the optimization automatically.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/690/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=690&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/07/project-euler-problem-21/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 20</title>
		<link>http://breadthfirst.wordpress.com/2010/07/06/project-euler-problem-20/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/06/project-euler-problem-20/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 09:16:59 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=684</guid>
		<description><![CDATA[Find the sum of the digits in the number 100! Not much to do here, after having solved problem 16.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=684&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>Find the sum of the digits in the number 100!</p></blockquote>
<p><pre class="brush: fsharp;">
    let ans = {1I..100I} |&gt; Seq.reduce(*) |&gt; Problem16.sumOfDigits
</pre></p>
<p>Not much to do here, after having solved <a href="http://breadthfirst.wordpress.com/2010/06/30/project-euler-problem-16/">problem 16</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/684/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/684/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/684/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=684&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/06/project-euler-problem-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 19</title>
		<link>http://breadthfirst.wordpress.com/2010/07/05/project-euler-problem-19/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/05/project-euler-problem-19/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 07:29:05 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=678</guid>
		<description><![CDATA[You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=678&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>You are given the following information, but you may prefer to do some research for yourself.</p>
<ul>
<li>1 Jan 1900 was a Monday.</li>
<li>Thirty days has September,<br />
April, June and November.<br />
All the rest have thirty-one,<br />
Saving February alone,<br />
Which has twenty-eight, rain or shine.<br />
And on leap years, twenty-nine.</li>
<li>A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.</li>
</ul>
<p>How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?</p></blockquote>
<p><pre class="brush: fsharp;">
module Problem19 =
    open System

    let start = new DateTime(1901, 1, 1)
    let finish = new DateTime(2000, 12, 31)

    let ans = start |&gt; Seq.unfold (fun (t : DateTime) -&gt; Some(t, t.AddMonths(1)))
                    |&gt; Seq.filter (fun (t : DateTime) -&gt; t.DayOfWeek = DayOfWeek.Sunday)
                    |&gt; Seq.takeWhile (fun (t : DateTime) -&gt; t &lt;= finish)
                    |&gt; Seq.length

</pre></p>
<p>Like all of the bigint project euler problems, the built in libraries make this completely trivial. Since my goal for this project is to learn F#/.NET, these trivial solutions are actually helpful.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/678/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=678&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/05/project-euler-problem-19/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 18</title>
		<link>http://breadthfirst.wordpress.com/2010/07/02/project-euler-problem-18/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/02/project-euler-problem-18/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 09:08:43 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=672</guid>
		<description><![CDATA[By starting at the top of the triangle below and moving to adjacent numbers on the row below, find the maximum total from top to bottom of the triangle below: 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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=672&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>By starting at the top of the triangle below and moving to adjacent numbers on the row below, find the maximum total from top to bottom of the triangle below:</p>
<p>75<br />
95 64<br />
17 47 82<br />
18 35 87 10<br />
20 04 82 47 65<br />
19 01 23 75 03 34<br />
88 02 77 73 07 63 67<br />
99 65 04 28 06 16 70 92<br />
41 41 26 56 83 40 80 70 33<br />
41 48 72 33 47 32 37 16 94 29<br />
53 71 44 65 25 43 91 52 97 51 14<br />
70 11 33 28 77 73 17 78 39 68 17 57<br />
91 71 52 38 17 14 91 43 58 50 27 29 48<br />
63 66 04 68 89 53 67 30 73 16 69 87 40 31<br />
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23</p>
<p><strong>NOTE:</strong> As there are only 16384 routes, it is possible to solve this problem by trying every route. However, <a href="index.php?section=problems&amp;id=67">Problem 67</a>, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)</p></blockquote>
<p>Like <a href="https://breadthfirst.wordpress.com/2010/06/28/project-euler-problem-14/">problem 14</a>, this problem, while brute forcable, just screams DP / memoization. Since I played around with memoization for 14, I decided to try a classic &#8216;C&#8217; style dynamic program this time around.</p>
<p><pre class="brush: fsharp;">
module Problem18 =
    open System
    let fileName = @&quot;C:\Users\d\Documents\Visual Studio 2010\Projects\Euler\Euler\euler 18.txt&quot;

    let aData = System.IO.File.ReadAllLines(fileName) |&gt; Array.rev |&gt;  Array.map (fun l -&gt;  l.Split([|' '|], System.StringSplitOptions.RemoveEmptyEntries) |&gt; Array.map System.Int32.Parse)
    let size = aData.Length
    let maxTable = Array2D.init size size (fun i j -&gt; if j &lt; size - i then aData.[i].[j] else 0)

    for i in 1 .. size - 1 do
        for j in 0 .. size - i - 1 do
            maxTable.[i,j] &lt;- maxTable.[i,j] + (max maxTable.[i-1,j] maxTable.[i-1,j+1])
    printfn &quot;Answer: %i&quot; maxTable.[size-1, 0]
</pre></p>
<p>There&#8217;s no reason to use a 2D array here, but since I already learned how to use 1D arrays, I figured I might as well try a 2D array, to learn something new.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/672/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=672&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/02/project-euler-problem-18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 17</title>
		<link>http://breadthfirst.wordpress.com/2010/07/01/project-euler-problem-17/</link>
		<comments>http://breadthfirst.wordpress.com/2010/07/01/project-euler-problem-17/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 07:54:43 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=667</guid>
		<description><![CDATA[How many letters would be needed to write all the numbers in words from 1 to 1000? I was just about to stop working through project Euler, since I hadn&#8217;t run across a problem that forced me to learn a new F# feature for a while (which was my motivation for doing this in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=667&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>How many letters would be needed to write all the numbers in words from 1 to 1000?</p></blockquote>
<p><pre class="brush: fsharp;">
module Problem17 =
    let ones = [|&quot;&quot;; &quot;one&quot;; &quot;two&quot;; &quot;three&quot;; &quot;four&quot;; &quot;five&quot;; &quot;six&quot;; &quot;seven&quot;; &quot;eight&quot;; &quot;nine&quot;; &quot;ten&quot;; &quot;eleven&quot;; &quot;twelve&quot;; &quot;thirteen&quot;; &quot;fourteen&quot;; &quot;fifteen&quot;; &quot;sixteen&quot;; &quot;seventeen&quot;; &quot;eighteen&quot;;  &quot;nineteen&quot;; &quot;twenty&quot;|] |&gt; Array.map String.length
    let tens = [|&quot;&quot;; &quot;&quot;;&quot;twenty&quot;; &quot;thirty&quot;; &quot;forty&quot;; &quot;fifty&quot;; &quot;sixty&quot;; &quot;seventy&quot;; &quot;eighty&quot;; &quot;ninety&quot;|] |&gt; Array.map String.length
    let hundred = [|&quot;hundred&quot;|] |&gt; Array.map String.length

    let rec numLength n =
        match n with
        | n when n &lt;= 20                    -&gt; ones.[n]
        | n when n &lt; 100                    -&gt; tens.[(n/10)] + (numLength (n%10))
        | n when n &lt; 1000 &amp;&amp; n % 100 = 0    -&gt; ones.[n/100] + hundred.[0]
        | n when n &lt; 1000                   -&gt; ones.[n/100] + hundred.[0] + String.length &quot;and&quot; + (numLength (n%100))
        | n when n = 1000                   -&gt; String.length &quot;oneThousand&quot;
        | _ -&gt; failwith &quot;Out of range&quot;

    let ans = {1..1000} |&gt; Seq.map numLength |&gt; Seq.sum
</pre></p>
<p>I was just about to stop working through project Euler, since I hadn&#8217;t run across a problem that forced me to learn a new F# feature for a while (which was my motivation for doing this in the first place), and the algorithmic aspect of the problems hasn&#8217;t been that interesting, but this problem forced me to use arrays, which I hadn&#8217;t used before. I guess I&#8217;ll keep going for at least another few problems.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/667/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=667&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/07/01/project-euler-problem-17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 16</title>
		<link>http://breadthfirst.wordpress.com/2010/06/30/project-euler-problem-16/</link>
		<comments>http://breadthfirst.wordpress.com/2010/06/30/project-euler-problem-16/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 09:22:50 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=662</guid>
		<description><![CDATA[What is the sum of the digits of the number 21000? The naive solution works well enough, but when IntelliSense revealed that bigint has a DivRem method, I had to try that. It&#8217;s twice as fast as the original solution, but a bit messier. This seems like the kind of thing that could be done [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=662&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>What is the sum of the digits of the number 2<sup>1000</sup>?</p></blockquote>
<p><pre class="brush: fsharp;">
module Problem16 =
    let sumOfDigits n =
        let rec loop m acc =
            if m = 0I then acc
            else loop (m/10I) (acc + (m%10I))
        loop n 0I

    let ans = 2I**1000 |&gt; sumOfDigits
</pre></p>
<p>The naive solution works well enough, but when IntelliSense revealed that bigint has a DivRem method, I had to try that.</p>
<p><pre class="brush: fsharp;">
    let rec sumOfDigits n =
        let rec loop m acc =
            if m = 0I then acc
            else
                let div,rem = bigint.DivRem (m,10I)
                loop div (acc + rem)
        loop n 0I
</pre></p>
<p>It&#8217;s twice as fast as the original solution, but a bit messier. This seems like the kind of thing that could be done automatically by a compiler optimization.</p>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:47px;width:1px;height:1px;overflow:hidden;">module Problem16 =<br />
let originalSumOfDigits n =<br />
let rec loop m acc =<br />
if m = 0I then acc<br />
else loop (m/10I) (acc + (m%10I))<br />
loop n 0I</p>
<p>let rec sumOfDigits n =<br />
let rec loop m acc =<br />
if m = 0I then acc<br />
else<br />
let div,rem = bigint.DivRem (m,10I)<br />
loop div (acc + rem)<br />
loop n 0I</p>
<p>let ans = 2I**1000 |&gt; sumOfDigits</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/662/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=662&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/06/30/project-euler-problem-16/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler in F#: Problem 15</title>
		<link>http://breadthfirst.wordpress.com/2010/06/29/project-euler-problem-15/</link>
		<comments>http://breadthfirst.wordpress.com/2010/06/29/project-euler-problem-15/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 09:58:49 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://breadthfirst.wordpress.com/?p=658</guid>
		<description><![CDATA[Starting in the top left corner of a 20&#215;20 grid, how many routes are there (without backtracking) to the bottom right corner? I&#8217;ve been trying to avoid solving these problems using math-oriented solutions, in order to get myself to actually do some programming and learn F#, but brute forcing this problem is so ridiculous that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=658&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>Starting in the top left corner of a 20&#215;20 grid, how many routes are there (without backtracking) to the bottom right corner?</p></blockquote>
<p>I&#8217;ve been trying to avoid solving these problems using math-oriented solutions, in order to get myself to actually do some programming and learn F#, but brute forcing this problem is so ridiculous that I couldn&#8217;t do it.</p>
<p><pre class="brush: plain;">
module Problem15 =
    let ans = ({21I .. 40I} |&gt; Seq.reduce(*)) / ({1I .. 20I} |&gt; Seq.reduce(*))
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/breadthfirst.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/breadthfirst.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/breadthfirst.wordpress.com/658/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=breadthfirst.wordpress.com&amp;blog=1814688&amp;post=658&amp;subd=breadthfirst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://breadthfirst.wordpress.com/2010/06/29/project-euler-problem-15/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Dan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
