 Thursday, April 24, 2008

For the past several months, I've been using F# to solve at least two Project Euler problems each week. I find this is a great way to sharpen my math skills and my F# skills simultaneously. If you're looking for a way to flex your programming muscles, you really should check out Project Euler.

Last week at the MVP Summit, my friend, Bill Wagner, pressured me to suggested that I post some of my solutions. Now, there are already plenty of smart people posting F# solutions to the Project Euler problems. That's why I've resisted starting my own series: I'm not certain that I have anything new to say on the topic. However, Bill was very convincing (especially when he mentioned that I would be starting a series to a couple hundred C# MVPs).

So, here's the deal. I will try to present the most beautiful solution that I can. Naturally, beauty is in the eye of the beholder, so you might disagree with me. That's OK. Just make certain to let me know why you disagree so that I can grow as a developer. If anything, this about learning to be a better programmer.

Let's get started.

Problem One

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.

Find the sum of all the multiples of 3 or 5 below 1000.

Obviously, Project Euler starts with some easy problems and moves up to harder fare. The first problem is pretty trivial. It's even reminiscent of the famous FizzBuzz question.

In F#, a list of all multiples of 3 or 5 less than 1000 can be created using a list comprehension. Once we have the list, it can be summed by folding with the addition operator.

[ for x in 1 .. 999 when x % 3 = 0 or x % 5 = 0 -> x ]
  |> List.fold_left (+) 0

The power of F# (and functional programming in general) is its ability to express problems in a more declarative way. This lends the language to mathematical problems very naturally. Looking at the solution above, there are some obvious changes that could make it more succinct. First, the duplicated modulo operation can be abstracted away by declaring a new operator. (Did I mention that F# allows you to declare new operators?) Second, we can extract the folding logic to a new function that better describes its intent.

let inline (/:) x y = x % y = 0 // evenly-divisible by...

let sum list = List.fold_left (+) 0 list

With these defined, we can express the solution more cleanly.

[ for x in 1 .. 999 when x /: 3 or x /: 5 -> x ] |> sum

That's beautiful.

posted on Thursday, April 24, 2008 12:37:46 PM (Pacific Standard Time, UTC-08:00)      Comments [3]
{"cc_re__cc_re__cc_by_3_0_us": ["ur happiness and well-being. Take the time to enjoy something else. You'll thank me later.<br />\r\n\t\t\t\t    </div>\r\n\t\t\t\t</div>\r\n\t\t\t\t<div class=\"sidebar\">\r\n\t\t\t\t\t<div class=\"sidecontent\">\r\n\t\t\t\t\t    <a href=\"http://www.dasblog.info\"><img src=\"http://diditwith.net/themes/dustin_default/dasBlog.jpg\" alt=\"dasBlog\" border=\"0\" /></a>\r\n\t\t\t\t\t\t<p><strong>Disclaimer</strong><br/>\r\nThe opinions expressed herein are my own personal opinions and do not represent\r\nmy employer's view in any way.</p>\r\n<br/>\r\n\t\t\t\t\t\t<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/3.0/us/\">\r\n<img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by/3.0/us/88x31.png\" />\r\n</a>\r\n<br />This work is licensed under a \r\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/3.0/us/\">Creative Commons Attribution 3.0 United States License</a>.<br/><br/>\r\n\t\t\t\t\t\t&copy; Copyright&nbsp;2015&nbsp;Dustin Campbell<br/><br/>\r\n\t\t\t\t\t\t<a href=\"Login.aspx\">Sign In</a><br>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n\t\t\t</div>\r\n\t\t</div>\r\n\t</div>\r\n</form></body>\r\n</html>\r\n", "\"http://diditwith.net/themes/dustin_default/dasBlog.jpg\" alt=\"dasBlog\" border=\"0\" /></a>\r\n\t\t\t\t\t\t<p><strong>Disclaimer</strong><br/>\r\nThe opinions expressed herein are my own personal opinions and do not represent\r\nmy employer's view in any way.</p>\r\n<br/>\r\n\t\t\t\t\t\t<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/3.0/us/\">\r\n<img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by/3.0/us/88x31.png\" />\r\n</a>\r\n<br />This work is licensed under a \r\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/3.0/us/\">Creative Commons Attribution 3.0 United States License</a>.<br/><br/>\r\n\t\t\t\t\t\t&copy; Copyright&nbsp;2015&nbsp;Dustin Campbell<br/><br/>\r\n\t\t\t\t\t\t<a href=\"Login.aspx\">Sign In</a><br>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n\t\t\t</div>\r\n\t\t</div>\r\n\t</div>\r\n</form></body>\r\n</html>\r\n"], "copyright__copyright__copyright": ["", "ator. (Did I mention that F# allows you to declare new operators?) Second, we can extract the folding logic to a new function that better describes its intent.\n\nlet\u00a0inline (/:) x y = x % y = 0 // evenly-divisible by...\n\nlet sum list = List.fold_left (+) 0 list\n\nWith these defined, we can express the solution more cleanly.\n\n[ for x in 1 .. 999 when x /: 3 or x /: 5 -> x ] |> sum\n\nThat's beautiful.\n\nposted on Thursday, April 24, 2008 12:37:46 PM (Pacific Standard Time, UTC-08:00)\u00a0 \u00a0 \u00a0 Comments [3]"]}