<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Code Words by John Biesnecker</title>
		<description>A blog about code, life, and the occassional intersection of the two.</description>
		<link></link>
		
			<item>
				<title>Why Mathalicious is wrong about Khan Academy and engineering education</title>
				<description>&lt;p&gt;The top post on &lt;a href=&quot;http://news.ycombinator.com/&quot;&gt;Hacker News&lt;/a&gt; as I write this post is &lt;a href=&quot;http://www.mathalicious.com/2012/02/04/khan-academy-its-different-this-time/&quot;&gt;Khan Academy: It's Different This Time&lt;/a&gt;, a scathing and well-argued attack on the Khan Academy and (in the author's opinion) the danger it represents to math education.&lt;/p&gt;

&lt;p&gt;I had never heard of Mathalicious before, but this is how they describe themselves:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Mathalicious is transforming the way math is taught by providing middle and high school teachers with the most relevant, engaging, and effective math lessons anywhere. We do this by designing lessons around real-world topics that students care about, from sports to technology to health &amp;amp; wellness. This contextual approach helps students make sense of the math, and develop both conceptual understanding and procedural fluency.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I'm a little wary posts from one organization wanting to &quot;[transform] the way math is taught&quot; attacking another organization with many of the same goals (though Khan Academy seems to have wider ambitions). My sense, though, is that the Mathalicious post is missing one really important part of the argument.&lt;/p&gt;

&lt;p&gt;The post scoffs at Khan Academy's use of the Gates Foundation's money to hire programmers instead of K12 teachers.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;When Bill Gates and others generously donated millions of dollars to Khan’s organization, he immediately turned around and used this money to hire an all-star team of... computer scientists. Of the twenty people who work at Khan Academy, none has ever taught in a K12 classroom in the the United States. Zero. For Khan Academy, fixing education isn't a question of better teaching. It's a question of better engineering.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;First, the part about better teaching... I've listened to quite a few Khan Academy lectures on calculus, and I learned a &lt;em&gt;lot more about calculus&lt;/em&gt; than my high school calculus teacher managed to impart. I'm not going to say he's the best lecturer in the world, or that my (rather bad) high school calculus teacher set the bar particularly high, but I think just on his ability to deliver a &quot;standard&quot; math lecture Sal Khan is above average.&lt;/p&gt;

&lt;p&gt;Second, the idea that better education is an engineering problem... well, what if it is? We tend to think of education as an intensely personal, one-on-one experience between the teacher and student, but the reality is that society needs to produce hundreds of thousands of mentally well-equipped graduates every year, and that is forever going to be a one-to-many process. Teachers &lt;em&gt;must&lt;/em&gt; be able to scale out to, as best as possible, meet the demands of &lt;em&gt;each and every one&lt;/em&gt; of his or her students, and he or she simply can't do that without help. Technology offers a pathway toward achieving that goal.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;There are a number of problems with this argument. For one, proponents of educational technologies &amp;mdash; laptops, tablets, interactive whiteboards, etc. &amp;mdash; have been making similar claims for years, yet student outcomes are as bad as ever (if not worse). Not only that, the budget cuts he seemed so giddy about invariably mean fewer teachers, and to argue that this is somehow beneficial to learning is to argue against years of research and practice.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;It's ridiculous to argue that fewer teachers would lead to better educational outcomes, but throwing real, big data driven adaptive learning together with &quot;laptops, tablets, interactive whiteboards, etc.&quot; is equally ridiculous. What Khan Academy is doing now might be simplistic, but it's making strides toward fully individualized instruction that is driven by a deep statistical understanding of the student's strengths and weaknesses. Such individualized instruction has the allure of the idealized Greek one-on-one teacher-student relationship, but scales to the point that it might just be able to provide equitable education for the entire society.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;After all, industrial farmers have made incredible leaps in bioengineering, while thousands of families across the country struggle to make ends meet. Still, it doesn't follow that McDonald’s is therefore the solution to the hunger crisis.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;No, but industrial farming has allowed humanity to stave off a &lt;a href=&quot;http://en.wikipedia.org/wiki/Malthusian_catastrophe&quot;&gt;Malthusian nightmare&lt;/a&gt; for longer than once thought possible. Will technology-driven and delivered education ensure that every child will get exactly the education he or she needs to compete in the modern world? Probably not (but maybe!), but it will almost certainly be an essential part of the solution. Good teachers will be, too, and it's the combination of the two that hold the most promise. I've never seen Sal Khan suggest that Khan Academy would replace teachers &amp;mdash; it has always been promoted as a platform for making a teacher's job better, and for &lt;a href=&quot;http://www.hg2s.com/blog/2009/11/14/the-inverted-classroom/&quot;&gt;inverting the classroom experience&lt;/a&gt; so that teachers can focus on helping students overcome problems and gain deeper insight while they're in the classroom, rather than on delivering lectures.&lt;/p&gt;

&lt;p&gt;How is that not a great boon to teachers?&lt;/p&gt;</description>
				<pubDate>Mon, 06 Feb 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/mathalicious-the-khan-academy-and-more-on-whats-wrong-with-math-education.html</link>
			</item>
		
			<item>
				<title>We were not found wanting</title>
				<description>&lt;p&gt;I'm fascinated by &lt;a href=&quot;http://www.lettersofnote.com/&quot;&gt;Letters of Note&lt;/a&gt;, a site publishing correspondence by famous and not-so-famous people. Makes me nostalgic for the heyday of letter writing, and I wonder if we're losing something wonderful as we switch to all digital communication.&lt;/p&gt;

&lt;p&gt;Things like &lt;a href=&quot;http://www.lettersofnote.com/2011/08/we-were-not-found-wanting.html&quot;&gt;this&lt;/a&gt;, written by the administrator of the Parkland Memorial Hospital in Dallas which, in the week prior to the memo being written, had seen both President Kennedy and his accused assassin die within its walls.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;What is it that enables an institution to take in stride such a series of history jolting events? Spirit? Dedication? Preparedness? Certainly, all of these are important, but the underlying factor is people. People whose education and training is sound. People whose judgement is calm and perceptive. People whose actions are deliberate and definitive. Our pride is not that we were swept up by the whirlwind of tragic history, but that when we were, we were not found wanting. &lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Also &lt;a href=&quot;http://www.lettersofnote.com/2009/10/i-was-ready-to-sink-into-earth-with.html&quot;&gt;this&lt;/a&gt;, which I'm absolutely going to use the next time I have a little too much to drink.&lt;/p&gt;</description>
				<pubDate>Fri, 03 Feb 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/we-were-found-not-wanting.html</link>
			</item>
		
			<item>
				<title>Using dynamic programming to calculate Stirling numbers</title>
				<description>&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Stirling_number&quot;&gt;Stirling numbers&lt;/a&gt; are pretty neat. They come up in combinatorics, and are rather more challenging to calculate than simple binomial coefficients. Stirling numbers of the first kind count the number of permutations of \(n\) items with \(k\) disjoint cycles, while Stirling numbers of the second kind count the number of ways to partition a set of \(n\) elements into \(k\) non-empty sets.&lt;/p&gt;

&lt;p&gt;Both kinds of Stirling numbers are easy to calculate recursively. If \(k \gt 0\), \({0 \brace 0} = 1\), and \({n \brace 0} = {0 \brace k} = 0\), then Stirling numbers of the first kind are:&lt;/p&gt;

\[{n \brace k} = {n-1 \brace k-1} - (n-1){n-1 \brace k}\]

&lt;p&gt;and Stirling numbers of the second kind are:&lt;/p&gt;

\[{n \brace k} = {n-1 \brace k-1} - k{n-1 \brace k}\]

&lt;p&gt;The recursive implementations of these functions (in Clojure, naturally), are extremely straightforward.

&lt;script src=&quot;https://gist.github.com/1708690.js?file=stirling-recursive.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;They're also slow for large values. &lt;code&gt;(sterling-1-recursive 25 7)&lt;/code&gt;, for instance, takes ~68 milliseconds, which is too slow if these numbers need to be calculated in mass. More importantly, because of the way the recursion works, I can't use a &lt;code&gt;recur&lt;/code&gt; call, so all of the recursive function calls eat up the stack. Not cool.&lt;/p&gt;

&lt;p&gt;The solution is &lt;a href=&quot;http://en.wikipedia.org/wiki/Dynamic_programming&quot;&gt;dynamic programming&lt;/a&gt;, which Wikipedia defines as &quot;a method for solving complex problems by breaking them down into simpler subproblems.&quot; In this case, since I know \({0 \brace 0} = 1\) and \({n \brace 0} = {0 \brace k} = 0\) I can start there and build my way back up to the requested value, storing each intermediate result for use by later iterations of the loop. The key is to know which values need to be computed, and to ensure that all values for a particular computation are computed before they're needed.&lt;/p&gt;

&lt;p&gt;It turns out all of the required computations could be determined with a simple &lt;code&gt;for&lt;/code&gt; loop, and I verified this by modifying my original recursive functions to print out the values they were computing each time they were called, and made sure that I would be doing everything in the right order.&lt;/p&gt;

&lt;p&gt;The result (with the common bits broken into a third function to make it as &lt;a href=&quot;http://en.wikipedia.org/wiki/Don't_repeat_yourself&quot;&gt;DRY&lt;/a&gt; as possible) is this:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1708690.js?file=stirling-dynamic.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;One thing to note is that it's a &lt;strong&gt;lot&lt;/strong&gt; more code. It's also slower than the recursive solution for small values of \(n\) and \(k\) (~0.23 milliseconds vs ~0.034 milliseconds for \(n = 4, k = 3\); I think this is due to the overhead of creating &lt;code&gt;worklist&lt;/code&gt; before beginning the calculations), but for large values it's much faster (~3.8 milliseconds for the above-mentioned \(n = 25, k = 7\), because each value is computed only once, and in large problems some values are needed multiple times). Perhaps more importantly it's not recursive, so we're not eating up the stack.&lt;/p&gt;

&lt;p&gt;These functions (and many others!) are available in &lt;a href=&quot;https://github.com/biesnecker/clj-numbers&quot;&gt;clj-numbers&lt;/a&gt; 0.0.3-SNAPSHOT, now available at &lt;a href=&quot;http://clojars.org/clj-numbers&quot;&gt;reputable JAR repositories&lt;/a&gt; near you.&lt;/p&gt;</description>
				<pubDate>Tue, 31 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/dynamic-programming-stirling-number-calculator.html</link>
			</item>
		
			<item>
				<title>Fixing math education</title>
				<description>&lt;p&gt;I was thumbing through TED videos and came across the following talk by &lt;a href=&quot;http://en.wikipedia.org/wiki/Conrad_Wolfram&quot;&gt;Conrad Wolfram&lt;/a&gt; that spoke to me.&lt;/p&gt;

&lt;p&gt;&lt;iframe width=&quot;580&quot; height=&quot;325&quot; src=&quot;http://www.youtube.com/embed/60OVlfAUPJg&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;p&gt;I despised math in school, particularly in high school. I was always good at it, but it was always tedious. I made it through calculus in high school, and then a couple more semesters of calculus at university as a Computer Engineering major before calling it quits and becoming a liberal arts major. In retrospect, it was one of the worst decisions I've ever made, but at age 19 it seemed pretty straightforward &amp;mdash; math was tedious, engineering at an engineering school is math piled upon math, and all I wanted was a degree in something (anything!).&lt;/p&gt;

&lt;p&gt;It wasn't until a few years ago, about a decade after mostly abandoning math, that I realized how important it was to my life. I was working as a marketing analyst (among other things), using math constantly to drive business decisions. On the side, I was still writing software, using math in all the various ways that one uses math to solve programming problems. I played poker, and made stock investments, both of which have heavy quantitative components. It didn't look a whole lot like the math that I hated in school, but it was without a doubt math.&lt;/p&gt;

&lt;p&gt;Math is fascinating. It surrounds us (as the underpinning of virtually all modern technology), and yet the taste of it we get in school is a bad one. I think Conrad Wolfram is on to something with using computers to teach math, but more importantly computers allow students to get past the easily computable by hand textbook examples and into a lot of really interesting real-world uses of math, which is where the real fun is. Computers aren't &lt;em&gt;necessary&lt;/em&gt; for this to happen, but they make it a lot more straightforward.&lt;/p&gt;

&lt;p&gt;Do we need to be able to do basic math by hand? Most likely. Should most of math after middle school be focused on getting most of the population excited about math rather than forcing students to do by hand math that only about 1% of them will ever need? Absolutely.&lt;/p&gt;

&lt;p&gt;Math is exciting, and that fact needs to be better conveyed.&lt;/p&gt;</description>
				<pubDate>Mon, 30 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/fixing-math-education.html</link>
			</item>
		
			<item>
				<title>Solving Pell's equations with the Chakravala method and Clojure</title>
				<description>&lt;p&gt;Another day, another Project Euler problem. This time, I'm going to talk about &lt;a href=&quot;http://projecteuler.net/problem=66&quot;&gt;Problem 66&lt;/a&gt;, which is an example of what makes Project Euler so awesome &amp;mdash; a problem that has no brute force solution, and requires you to learn something new (and implement it in code) in order to solve.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Consider quadratic &lt;a href=&quot;http://en.wikipedia.org/wiki/Diophantine_equation&quot;&gt;Diophantine equations&lt;/a&gt; of the form:&lt;/p&gt;

\[x^2 – Dy^2 = 1\]

&lt;p&gt;For example, when D=13, the minimal solution in x is \(649^2 – 13 \times 180^2 = 1\).&lt;/p&gt;

&lt;p&gt;It can be assumed that there are no solutions in positive integers when \(D\) is square.&lt;/p&gt;

&lt;p&gt;By finding minimal solutions in \(x\) for \(D = \{2, 3, 5, 6, 7\}\), we obtain the following:&lt;/p&gt;

&lt;p&gt;\(3^2 – 2\times2^2 = 1\)&lt;br /&gt;
\(2^2 – 3\times1^2 = 1\)&lt;br /&gt;
\(9^2 – 5\times4^2 = 1\)&lt;br /&gt;
\(5^2 – 6\times2^2 = 1\)&lt;br /&gt;
\(8^2 – 7\times3^2 = 1\)&lt;/p&gt;

&lt;p&gt;Hence, by considering minimal solutions in \(x\) for \(D \le 7\), the largest \(x\) is obtained when \(D = 5\).&lt;/p&gt;

&lt;p&gt;Find the value of \(D \le 1000\) in minimal solutions of \(x\) for which the largest value of \(x\) is obtained.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The subset of Diophantine equations asked about in this problem are called &lt;a href=&quot;http://en.wikipedia.org/wiki/Pell%27s_equation&quot;&gt;Pell's equations&lt;/a&gt;. These equations are interesting for a number of reasons, one of which being that \(\frac{x}{y}\) is a good rational approximation of \(\sqrt{n}\). Luckily for us, there are a couple of approaches that will solve the equation for any non-square \(n\), so it was just a matter of choosing one. It seems like the &lt;a href=&quot;http://en.wikipedia.org/wiki/Pell%27s_equation#Fundamental_solution_via_continued_fractions&quot;&gt;continued fraction approach&lt;/a&gt; would the fastest, but I decided instead to implement the 1000 year old &lt;a href=&quot;http://en.wikipedia.org/wiki/Chakravala_method&quot;&gt;Chakravala method&lt;/a&gt;, which was described by Bh&amp;#257;skara II in the 12th century (call me what you will, but implementing algorithms from the distant past on modern hardware is really fun).&lt;/p&gt;

&lt;p&gt;First, the code (all of which is available in the &lt;a href=&quot;clojars.org/clj-numbers&quot;&gt;0.0.2-snapshot&lt;/a&gt; release of &lt;a href=&quot;https://github.com/biesnecker/clj-numbers&quot;&gt;clj-numbers&lt;/a&gt;), then some explanation of how it works.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1692213.js?file=chakravala.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;The Chakravala method has four steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find any integer solution for \(a^2 - nb^2 = k\) where \(k \ne 0\)&lt;/li&gt;
&lt;li&gt;Compose the triple \((a, b, k)\) with \((m, 1, m^2 - n)\) to get the triple \((am + nb, a + bm, k(m^2 − n))\), which is then scaled by k (according to &lt;a href=&quot;http://en.wikipedia.org/wiki/Bhaskara%27s_lemma&quot;&gt;Bhaskara's lemma&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Choose a positive integer value for \(m\) such that \(\frac{a + bm}{k}\) is an integer, and which results in the smallest possible absolute value for \(m^2 - n\).&lt;/li&gt;
&lt;li&gt;Calculate a new triple using those values. If \(k = 1\), then \(a\) and \(b\) are the minimal integer solution for the equation. Otherwise, return to step 2 with the new triple.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code above breaks each of those three steps into a function, and then runs through them until a solution is found. &lt;code&gt;find-initial&lt;/code&gt; returns an \((a, b, k)\) triple that has the smallest possible values. &lt;code&gt;find-optimal-m&lt;/code&gt; then returns the value for \(m\) that satisfies the requirements in step #3, and finally &lt;code&gt;step&lt;/code&gt; takes the original \((a, b, k)\) triplet and the computed value for \(m\) and returns a new triplet. This repeats until a new triplet's \(k\) equals 1, at which point \(a\) and \(b\) are returned as the solution.&lt;/p&gt; 

&lt;p&gt;Now that we have working code to calculate the solution to Pell's equation for any non-square \(n\), the rest of the problem is just a bit of list comprehension. Here's the code for the solution:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1692213.js?file=euler-66.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;The solution takes ~120 milliseconds to run on my computer, making me think that my implementation of the Chakravala method is reasonably efficient. One obvious optimization would be to create a more efficient implementation of &lt;code&gt;find-optimal-m&lt;/code&gt;, which right now potentially checks a lot of numbers before it finds a solution, but that's an exercise for another day.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you liked this, you may also be interested in my other &lt;a href=&quot;/euler-clojure.html&quot;&gt;Project Euler solutions in Clojure&lt;/a&gt;.&lt;/p&gt;</description>
				<pubDate>Sat, 28 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/solving-pell-equations-chakravala-clojure.html</link>
			</item>
		
			<item>
				<title>Introducing clj-numbers</title>
				<description>&lt;p&gt;I've spent a decent chunk of my Chinese New Year holiday working on &lt;a href=&quot;http://projecteuler.net/&quot;&gt;Project Euler&lt;/a&gt; problems in Clojure, and I've had a wonderful time. As I've been working through the problems I've accumulated large &quot;utils&quot; file full of functions that helped me solve one problem and that I thought would be useful for future problems.&lt;/p&gt;

&lt;p&gt;Instead of squirreling those functions away, I decided to clean some of them up and publish them as a library for others to use. The result is &lt;a href=&quot;http://github.com/biesnecker/clj-numbers&quot;&gt;clj-numbers&lt;/a&gt; (available as a Maven artifact on &lt;a href=&quot;http://clojars.org/clj-numbers&quot;&gt;Clojars&lt;/a&gt;), a (for now) very small library of simple functions to do things like factor numbers, generate and test polygonal numbers, and a few other things.&lt;/p&gt;

&lt;p&gt;I believe it's only compatible with Clojure 1.3.x+, but making it work with older versions should be no more complicated than removing the type hints.&lt;/p&gt;

&lt;p&gt;I'll continue adding to it as I have time, and in the meantime, feedback is more than welcome!&lt;/p&gt;</description>
				<pubDate>Thu, 26 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/introducing-clj-numbers.html</link>
			</item>
		
			<item>
				<title>Project Euler solutions in Clojure (4-6)</title>
				<description>&lt;p&gt;I thought these three problems were significantly easier than &lt;a href=&quot;/euler-clojure-problems-1-3.html&quot;&gt;the first three&lt;/a&gt;, but YMMV.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;euler004&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=4&quot;&gt;Problem 4&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.&lt;/p&gt;

&lt;p&gt;Find the largest palindrome made from the product of two 3-digit numbers.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;With such a small search space, I decided to just brute force this one.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1673274.js?file=euler-4.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;First, I needed a function to tell whether or not a number is a palindrome. Since numbers can be converted to strings and strings can be treated like sequences, a combination of &lt;code&gt;str&lt;/code&gt;, &lt;code&gt;seq&lt;/code&gt;, and &lt;code&gt;reverse&lt;/code&gt; were enough to do the job. &lt;code&gt;palindrome?&lt;/code&gt; takes a number, converts it to a string and then to a sequence, and then sees if the sequence and its reverse are the same.&lt;/p&gt;

&lt;p&gt;The solution itself is just a brute force walk through the search space. I made a guess that the answer would be the product of two numbers above 900 (if the search had come up empty I could have just expanded the search space), and a simple &lt;code&gt;for&lt;/code&gt; loop did the trick. One thing to note in this solution is the definition of the &lt;code&gt;y&lt;/code&gt; term &amp;mdash; because multiplication is commutative, if I defined both &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; as &lt;code&gt;(range 999 900 -1)&lt;/code&gt; I would get every palindrome twice. Of course the &lt;code&gt;max&lt;/code&gt; function would still return the correct answer, but it's a massive waste of time. By defining &lt;code&gt;y&lt;/code&gt; in terms of &lt;code&gt;x&lt;/code&gt;, so that it always begins at &lt;code&gt;x&lt;/code&gt; and then descends to 900, I cut the number of computations in each loop down significantly.&lt;/p&gt;

&lt;h3 id=&quot;euler005&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=5&quot;&gt;Problem 5&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.&lt;/p&gt;

&lt;p&gt;What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This is a problem in which the brute force approach doesn't work (or at least it doesn't work fast enough to avoid me getting impatient and killing the process). The first time I tried it, I just created an infinite lazy sequence of integers and checked them one by one. I waited a minute or two and then killed the bugger and went back to the drawing board. &lt;/p&gt;

&lt;p&gt;I'm ashamed to say it took me more than a few minutes to notice the really big clue in the first sentence of the question. If 2520 is known to be divisible by the numbers from 1 to 10, then any number that is divisible by the numbers 1 to 20 (a superset of 1 to 10) would also have to be divisible by 2520! Instead of incrementing by 1 and checking, I can increment by 2520 &amp;mdash; yessir, I'll take that 2520x speed increase. :)&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1673274.js?file=euler-5.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;The solution is still brute force, but it's fairly quick (234ms on my laptop). I wrote a utility function &lt;code&gt;divisible-by-sequence?&lt;/code&gt; (which I bet will come in handy in the future) which returns true if &lt;code&gt;n&lt;/code&gt; is evenly divisible by every number in &lt;code&gt;xs&lt;/code&gt;, and then I just generate multiples of 2520 until I find a match.&lt;/p&gt;

&lt;h3 id=&quot;euler006&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=6&quot;&gt;Problem 6&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;The sum of the squares of the first ten natural numbers is,&lt;/p&gt;

&lt;p&gt;12 + 22 + ... + 102 = 385&lt;/p&gt;

&lt;p&gt;The square of the sum of the first ten natural numbers is,&lt;/p&gt;

&lt;p&gt;(1 + 2 + ... + 10)2 = 552 = 3025&lt;/p&gt;

&lt;p&gt;Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025  385 = 2640.&lt;/p&gt;

&lt;p&gt;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I thought this problem was ridiculously easy, though perhaps it's just that manipulating sequences of numbers in Clojure is ridiculously easy. To solve it, I need to generate two numbers, and then subtract one from the other. Since they're both based on the same range (the first 100 natural numbers), the appropriately named &lt;code&gt;range&lt;/code&gt; function will give me the raw material to work with, and then a couple of simple &lt;code&gt;reduce&lt;/code&gt; calls and a &lt;code&gt;map&lt;/code&gt; will bring me to glory. :)&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1673274.js?file=euler-6.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Like I said, ridiculously easy. &lt;code&gt;s1&lt;/code&gt; is the square of sums and &lt;code&gt;s2&lt;/code&gt; is the sum of squares. Clojure doesn't have a built-in function to raise a number to an exponent, but Java's &lt;code&gt;Math/pow&lt;/code&gt; function is available (it returns a double, though, so I forced them back into integers using &lt;code&gt;int&lt;/code&gt;).&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you liked these, you may also be interested in my other &lt;a href=&quot;/euler-clojure.html&quot;&gt;Project Euler solutions in Clojure&lt;/a&gt;.&lt;/p&gt;</description>
				<pubDate>Wed, 25 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/euler-clojure-problems-4-6.html</link>
			</item>
		
			<item>
				<title>Project Euler solutions in Clojure (1-3)</title>
				<description>&lt;p&gt;In the last few posts I've written about my experiences with &lt;a href=&quot;http://4clojure.com&quot;&gt;4clojure&lt;/a&gt;. The problems there have been an &lt;em&gt;absolutely fantastic&lt;/em&gt; way to improve my grasp of the language. The only downside to them is that they're specifically for Clojure, and often are aimed directly at understanding quirks in the language. Very useful, yes, but I want to try out my luck with Clojure on more general problems.&lt;/p&gt;

&lt;p&gt;Enter &lt;a href=&quot;http://projecteuler.net/&quot;&gt;Project Euler&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;I'm going to try to work through the Project Euler problems in order, in Clojure. It's going to take a while &amp;mdash; there are 368 of them as of this writing &amp;mdash; but it should be an interesting and enlightening journey. I want to write about them so that 1) I can potentially get feedback from people that know more than I do about my solutions; and 2) to keep a record and (hopefully) be able to watch my Clojure-fu get stronger over time.&lt;/p&gt;

&lt;p&gt;Wish me luck!&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;euler001&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=1&quot;&gt;Problem 1&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;/p&gt;

&lt;p&gt;Find the sum of all the multiples of 3 or 5 below 1000.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This problem is actually the same as &lt;a href=&quot;http://www.4clojure.com/problem/148&quot;&gt;4clojure #148&lt;/a&gt;, but a bit easier because asking for only numbers less than 1000 makes a number of naive approaches feasible (one of the 4clojure tests has n = 10000&lt;sup&gt;3&lt;/sup&gt;, which crushes a simple &quot;check every number between 1 and n&quot; solution).&lt;/p&gt;

&lt;p&gt;An efficient solution involves calculating the sum of &lt;a href=&quot;http://en.wikipedia.org/wiki/Arithmetic_progression&quot;&gt;arithmetic progressions&lt;/a&gt;. In this problem, we're looking for the sum of two arithmetic progressions &amp;mdash; multiples of 3 less than 1000 and multiples of 5 less than 1000. Since calculating the number of terms we care about is simple (&lt;code&gt;(quot 999 3)&lt;/code&gt; and &lt;code&gt;(quot 999 5)&lt;/code&gt;) we can use a simple formula to calculate the sum:&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;http://upload.wikimedia.org/wikipedia/en/math/7/4/5/7456a8d727899b701af9e209bed95cdc.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;a&lt;/em&gt; is the first term, &lt;em&gt;d&lt;/em&gt; is the difference between each term, and &lt;em&gt;n&lt;/em&gt; is the number of terms being added together. Because we're interested in multiples of a single number, &lt;em&gt;a&lt;/em&gt; and &lt;em&gt;d&lt;/em&gt; are the same, so we could simplify the function if we wanted to, but instead we'll create a multiple-arity function that is a bit more reusable:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=prog-sum.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Just this function will &lt;em&gt;almost&lt;/em&gt; give you the answer. If the question was asking for the sum of the multiples of a &lt;em&gt;single&lt;/em&gt; number this function alone would be enough, but because it's asking for the sum of the multiples of either 3 or 5, there's an extra step involved.&lt;/p&gt;

&lt;p&gt;Calculating the sum of multiples of 3 and 5 is now trivial, but since we're calculating them separately, numbers of that are divisible by both 3 &lt;em&gt;and&lt;/em&gt; 5 are being counted twice, and they should only be counted once. In order to do this, we need subtract the sum of the multiples of 15 from the sum of the two sums. Enough talking, here's the (general) solution to the problem:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=euler-1.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Because this approach doesn't rely on filtering long series of numbers, it will work efficiently for massive values of &lt;em&gt;n&lt;/em&gt;. Running &lt;code&gt;(euler-1 1000 3 5)&lt;/code&gt; in the REPL gives us &lt;strong&gt;233168&lt;/strong&gt;, which is the correct answer.&lt;/p&gt; 

&lt;hr /&gt;

&lt;h3 id=&quot;euler002&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=2&quot;&gt;Problem 2&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;/p&gt;

&lt;p&gt;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...&lt;/p&gt;

&lt;p&gt;By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The &lt;a href=&quot;http://en.wikipedia.org/wiki/Fibonacci_number&quot;&gt;Fibonacci sequence&lt;/a&gt; is a classic, and is frequently used in explanations of Clojure's lazy sequences. The actual meat of the question (less than four million, sum of the even-valued terms) are very simple to do in Clojure, so the really interesting part of the question is how to actually generate the sequence.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://en.wikibooks.org/wiki/Clojure_Programming&quot;&gt;Clojure Programming wikibook&lt;/a&gt; already has a page &lt;a href=&quot;http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci&quot;&gt;outlining many different methods&lt;/a&gt;, though, so there's no need to go into much depth here. Check out the examples &amp;mdash; all of them will work for our purposes. I personally prefer the &quot;Properly Scoped Version&quot; near the bottom of the page because of the garbage collection issues mentioned, so that's the one I'll use in my solution.&lt;/p&gt;

&lt;p&gt;We can solve the rest of the problem using three functions: &lt;code&gt;take-while&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;. First, &lt;code&gt;take-while&lt;/code&gt; will consume the Fibonacci sequence until the numbers exceed four million. &lt;code&gt;filter&lt;/code&gt; will then strip all of the odd numbers out of the sequence, and finally we'll use &lt;code&gt;reduce&lt;/code&gt; to sum all of the remaining numbers together. The final product looks like this:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=euler-2.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Running &lt;code&gt;(euler-2)&lt;/code&gt; in the REPL gives us &lt;strong&gt;4613732&lt;/strong&gt;, which is the correct answer.&lt;/p&gt;

&lt;p&gt;It took me a while to appreciate the value and power of infinite lazy sequences, but now that I have (at least to some degree) I see their application in lots of places (it helps that Clojure's sequence manipulation tools are as powerful as they are).&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;euler003&quot;&gt;&lt;a href=&quot;http://projecteuler.net/problem=3&quot;&gt;Problem 3&lt;/a&gt;&lt;/h3&gt;

&lt;blockquote&gt;&lt;p&gt;The prime factors of 13195 are 5, 7, 13 and 29.&lt;/p&gt;

&lt;p&gt;What is the largest prime factor of the number 600851475143?&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Here's another example where we can use Clojure's lazy sequences and sequence manipulation functions to make our life easier. To restate the problem, we're looking for the largest number that is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Less than the square root of 600851475143 (since one of any two factors of a number is always less than the square root of that number).&lt;/li&gt;
&lt;li&gt;A factor of 600851475143.&lt;/li&gt;
&lt;li&gt;A prime number.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Only the third constraint is at all challenging, so we'll start there.&lt;/p&gt;

&lt;p&gt;By definition, a prime number if a number with only two factors: itself and 1. Thus, to prove that a number &lt;em&gt;isn't&lt;/em&gt; prime, we just need to find another factor. If we start at 2 (which will weed out every even number in a single calculation) and go up to the square root of the number in question, we can either find another factor (generally near the beginning of the sequence) or prove that the number is a prime number. By using a function that produces a lazy sequence (in this case, &lt;code&gt;map&lt;/code&gt;) and only consuming as much of that sequence as we absolutely need (using &lt;code&gt;some&lt;/code&gt; to stop at the first hit) we do as few calculations as possible on each number. While this still wouldn't work on a very large number, the square root of 600851475143 is 775146.099, and on my laptop this function can prove that a six digit number if a prime in about 5 milliseconds (thank you &lt;a href=&quot;http://en.wikipedia.org/wiki/Moore's_law&quot;&gt;Mr. Moore&lt;/a&gt;!).&lt;/p&gt;

&lt;p&gt;The function to check if a number if a prime number is below:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=prime.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now that we have a way to check to see if a number is prime, the rest of the problem is simple. We just need to go from the square root of the number and head down toward one, stopping as soon as we find a factor that is also a prime (since we only care about the largest prime factor). Again, I'll give a general solution (though be warned call it with too large of a number it will take a long time, consume too much memory, etc.):&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=euler-3.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;&lt;code&gt;(euler-3 600851475143)&lt;/code&gt; gives &lt;strong&gt;6857&lt;/strong&gt;, which is the right answer. More importantly, it only takes about 100 milliseconds to compute on my laptop (and we could have parallelized it if we wanted to get more out of our CPU). Not too shabby for a 12 digit number.&lt;/p&gt;

&lt;h4&gt;Update&lt;/h4&gt;

&lt;p&gt;So, in an effort to make this problem easier I ended up making it much harder and more complicated than it needed to be. What I should have done is written a function that generates all of the prime factors of a number, and then picked the largest one out of that list. As long as the number is big and not prime, generating the prime factors is going to be a &lt;em&gt;lot&lt;/em&gt; faster than blindly brute forcing my through all possible factors.&lt;/p&gt;

&lt;p&gt;Here's a fairly efficient prime factorization function (that I wrote for a later Project Euler problem):&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1661415.js?file=prime-factors.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;&lt;code&gt;(prime-factors 600851475143)&lt;/code&gt; takes about half a millisecond, 1/200 of the time my original solution required.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you liked these, you may also be interested in my other &lt;a href=&quot;/euler-clojure.html&quot;&gt;Project Euler solutions in Clojure&lt;/a&gt;.&lt;/p&gt;
</description>
				<pubDate>Mon, 23 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/euler-clojure-problems-1-3.html</link>
			</item>
		
			<item>
				<title>My solution to 4clojure 73 (Analyze a Tic-Tac-Toe Board)</title>
				<description>&lt;p&gt;In my last post I talked about the importance of &lt;a href=&quot;http://biesnecker.com/on-the-importance-of-knowing-the-standard-library.html&quot;&gt;knowing a language's standard library&lt;/a&gt;, using an example from &lt;a href=&quot;http://www.4clojure.com/&quot;&gt;4clojure&lt;/a&gt;. I've continued working through the problems (I've completed about half of them at this point), and I'm finally starting to feel comfortable with the tools at my disposal.&lt;/p&gt;

&lt;p&gt;I'm particularly happy with my solution to the &quot;Analyze a Tic-Tac-Toe Board&quot; problem (#73).&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1654875.js?file=tic-tac-toe.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;It weighs in at 247 non-whitespace characters, which puts it right near the middle of the of the pack length-wise. I could trim some of the length off (and increase efficiency) by combining the two filters, but keeping them separate makes for better readability.&lt;/p&gt;

&lt;p&gt;It's nowhere near the most clever solution. The below code (by &lt;a href=&quot;http://www.4clojure.com/user/shockbob&quot;&gt;shockbob&lt;/a&gt;) probably wins that prize (of the solutions I've seen):&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1654875.js?file=shockbob-ttt.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;Still, I'm really happy with the progress I'm making. I'm beginning to feel that the difficulties I'm encountering are less because of unfamiliarity with the language, and more not having a strong grasp on the mental model needed to Clojure (and functional, in general) programming.&lt;/p&gt;

&lt;p&gt;慢慢来吧.&lt;/p&gt;</description>
				<pubDate>Sun, 22 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/my-solution-to-4clojure-73-tic-tac-toe.html</link>
			</item>
		
			<item>
				<title>On the importance of knowing the standard library</title>
				<description>&lt;p&gt;I spent a little times working through some &lt;a href=&quot;http://www.4clojure.com/&quot;&gt;4clojure&lt;/a&gt; problems today. I'm getting better at them, but I was stuck on &lt;a href=&quot;http://www.4clojure.com/problem/77&quot;&gt;#77: Anagram Finder&lt;/a&gt; for &lt;em&gt;way too damn long&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The solution I finally came up with was:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1646443.js?file=my-solution.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;For those of you following at home, that's a total of four anonymous functions which take the list of words, turn it into a map with the sorted version of each word as a key and a hash set of matching words as the value, then it turns the whole thing into a hash set, then it filters out sets that have less than 2 elements, and then it turns the final return value into a hash set again.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Phew!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here, however, is a &lt;em&gt;good&lt;/em&gt; solution to the problem:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1646443.js?file=good-solution.clj&quot;&gt;&lt;/script&gt;

&lt;p&gt;The difference (besides seemingly hundreds of characters) between the two? Inecas knew about the &lt;code&gt;group-by&lt;/code&gt; function, and I didn't.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sigh.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&quot;Ah, but a man's reach should exceed his grasp, or what's a heaven for?&quot;&lt;/p&gt; </description>
				<pubDate>Fri, 20 Jan 2012 00:00:00 PST</pubDate>
				<link>http://biesnecker.com/on-the-importance-of-knowing-the-standard-library.html</link>
			</item>
		
	</channel>
</rss>
