Project Euler in Clojure – Problem 5

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.  My approach to Problem 4 can be found here.

Problem 5 of Project Euler is described as:

What is the smallest positive number that is evenly
divisible by all of the numbers from 1 to 20?

In reading this problem I realized that this is just another way to state:

Find the Least Common Multiple of all of the numbers between 1 and 20.

The solution I came up with is:

(ns project-euler.core
  (:require [clojure.string :as string]
            clojure.math.numeric-tower))

(defn problem5
  ([] (problem5 20))
  ([n] (reduce lcm 1 (range 2 (inc n)))))

I found that previous to Clojure 1.2 there was a math library in Clojure-Contrib, which after 1.2 is now been moved to clojure.math.numeric-tower, so that now gets included in the vector of libraries in the :require of the ns function.

Once I had this, it was now just as simple as calling the reduce function with the function lcm, feeding it a initialization value of 1 to the reduce function and having that operate on the range of numbers from 2 to the number bound to the var n, which when called without parameters will use the value of twenty as specified in the problem definition. By defining the different arities for the function problem5, it allowed me to call the function with different numbers to the results of the function, as the full description gave the solution for the numbers in the range of 1 through 10.

Again, I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Update**
My solution to Problem 6 has been posted here.

–Proctor

Setting up PHP on IIS 7.0

Today is the last day of our previous UX developer David Belcher, since his contract is up and my company has hired a permanent UX developer. David had setup a reference site for the different projects he was involved in, but it is currently hosted on one of his consultancy company sites. The content should stay up for as long as we will need it, but as soon as we are going to need to make changes to the reference styles, or any of the mock-ups, it will be out of date, as our UX developer will not have access the site to make changes.

To help ease this transition, yesterday I offered to see if we could get it setup somewhere where it could live and be updated. The catch was that wherever were would put it up would need to be able to run PHP. I found some information on IIS 7.0 being able to host PHP, so we decided to host it locally on an internal development web-server until we can determine a permanent place where we would like it to live.

I did a Google search and found that there is a whole sub-domain for PHP on the IIS.net site. Right there at the top of the page was a link to a Microsoft Web Platform Installer for PHP. That wound up being a straight forward setup, but one of the pages was throwing an error and not having any PHP experience, other than reading a few PHP files, I wasn’t sure what was going on.

I found a couple of resources directing me to the display_errors setting in the PHP config file php.ini. As this is an internal development box, I felt comfortable enough to turn this flag to On, since to access the server one has to be on our VPN, and the security risk for a internal PHP site that is for reference of styles and workflow would be pretty low risk. So I made the change to the php.ini file and restarted IIS. When I hit the page, I was now getting:

Parse Error: syntax error, unexpected $end in [somefile].php on line [x]

Cool, I was one step closer to figuring out what was happening instead of a generic server error. Now it was time to figure out what was causing the syntax error. After a while of trying modify the file by trying to strategically remove sections, followed by searching for the generic form of my specific message, I came across this post on the site My Digitial Life. After going back in to the php.ini file and setting short_open_tag to On and restarting IIS, the page came to life.

I realized after doing all of this yesterday, that I should document this somewhere, and as we don’t have a good place to track this internally, I would post these steps so that someone else might benefit from them, instead of just being buried in a personal composition book of work related notes.

Hope this can help someone out there when they encounter a similar situation.

**Update**
For those unfamiliar with PHP, to make sure that it is working, and to check the version you can create a test file that you can hit to get version and deployment info on your PHP setup. Just create a file test.php with the following in it:

<?php  phpinfo(); ?>

Then hit the page test.php and you should see something along the lines of:
test.php result sample

Project Euler in Clojure – Problem 4

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.  My approach to Problem 3 can be found here.

Problem 4 of Project Euler is described as:

Find the largest palindrome made from the product of two 3-digit numbers.

The solution I came up with is:

(ns project-euler.core
  (:require [clojure.string :as string]))

(defn is-palindrome? [s]
 (= (str s) (string/join (reverse (str s)))))

(defn problem4 []
 (apply max (filter is-palindrome? (for [x (range 100 1000) y (range 100 1000)] (* x y)))))

As I want to use the join function in the clojure.string namespace, I added the :require keyword and aliased clojure.string as string using the :as keyword from the namespace macro.

The function is-palindrome? converts the value into a string, reverses the stream of characters in the string and then joins them back together and then compares it with the value as a string.

As defined problem4 does the meat of the work, by using a for loop over the set of of values from 100 to 999 for the binding of x and y, and multiplies x and y for each combination. This set is then filtered against the is-palindrome function, and the filtered sequence is then passed to the max function via apply.

Again, I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Update**
My solution to Problem 5 has been posted here.

–Proctor

Project Euler in Clojure – Problem 3

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.  My approach to Problem 2 can be found here.

Problem 3 of Project Euler is described as:

What is the largest prime factor of the number 600851475143 ?

For this problem, I needed to generate the prime factors of a number. In order to generate the prime factors, I decided to pul out the Uncle Bob’s (found here, here, here, and here) Prime Factors Kata and apply it to Clojure. By doing this kata, I test drove the result, and decided I would give Brian Marick’s Midje a shot and play with that some as well. The resulting tests are as follows.

(ns project-euler.prime-factors-test
  (:use clojure.test
        midje.sweet
        project-euler.core))

(defn mersenne [n]
  (int (dec (Math/pow 2 n))))

(fact
  (prime-factors-of 1) => []
  (prime-factors-of 2) => [2]
  (prime-factors-of 3) => [3]
  (prime-factors-of 4) => [2 2]
  (prime-factors-of 5) => [5]
  (prime-factors-of 6) => [2 3]
  (prime-factors-of 7) => [7]
  (prime-factors-of 8) => [2 2 2]
  (prime-factors-of 9) => [3 3]
  (prime-factors-of 10) => [2 5]
  (prime-factors-of 11) => [11]
  (prime-factors-of 12) => [2 2 3]
  (prime-factors-of 16) => [2 2 2 2]
  (prime-factors-of 25) => [5 5]
  (prime-factors-of (* 2 3 5 7 11 17 37)) => [2 3 5 7 11 17 37]
  (prime-factors-of (mersenne 17)) => [(mersenne 17)])

The total solution to problem 3 is as follows, with the test driven function for generating the prime factors, is:

(defn factors-starting-at [f n]
  (cond (= n 1) []
        (zero? (rem n f)) (cons f (factors-starting-at f (/ n f)))
        :else (recur (inc f) n)))

(defn prime-factors-of [n]
  (factors-starting-at 2 n))

(defn problem3 []
  (reduce max (prime-factors-of 600851475143)))

The resulting function for prime-factors-of calls the recursive function factors-starting-at with the first factor of 2, and the number itself. The factors-starting-at returns an empty vector when n is one. When the factor is actually a factor of the number (zero? (rem n f)) the returned result is the factor cons’ed onto the result of calling factors-starting-at again with the same factor, and the number divided by that factor. Otherwise, we return the result of recur-ing with the same number and the factor incremented again.

The solution to problem 3 is actually very simple in Clojure after one generates the prime factors for a number. All that one has to do is reduce the sequence of prime factors with the function max to find the largest prime factor for the number 600851475143.

I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Updated**
Here is my approach to Problem 4 in Clojure.

–Proctor

Project Euler in Clojure – Problem 2

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.  My approach to Problem 1 can be found here.

Problem 2 of Project Euler is described as:

By considering the terms in the Fibonacci sequence whose values do
not exceed four million, find the sum of the even-valued terms.

The solution I came up with is:

(defn skip-n [n s]
  (cond
    (<= n 0) s
    :else (recur (dec n) (rest s))))

(defn fib
  ([] (concat [0N 1N] (fib 0N 1N)))
  ([a b] (let [c (+ a b)]
          (lazy-seq
            (cons c (fib b c))))))

(defn problem2 []
  (reduce + (filter #(even? %) (take-while #(< % 4000000N) (skip-n 2 (fib))))))

As a didn’t see a skip function, which would keep a sequence, but skip over a given number of inputs, I created the function skip-n. The skip function calls the function rest on the sequence n number of times by using recur, and decrementing n.

The function fib generates the Fibonacci sequence lazily by using the function lazy-seq to create a lazily evaluated sequence, as I want the numbers generated until the time that I decide I do not need them anymore. Instead of trying to determine how many that will be before I generate them, I just decided to make them generated lazily.

The previous two functions were a helper function, and the function to generate the Fibonacci sequence (huh, sequence is even in the name). The work to determine the results of the problem are found in the function problem2. This function uses the skip-n function to skip the first two results, as Project Euler shows the Fibonacci sequence starting as 1, 2, 3, … instead of the starting as 0, 1, 1, 2, 3, … so I skip the first two in the sequence. I then use the take-while function to take only those items which are less than 4 million, which I then feed to the filter function to take only those items from the Fibonacci sequence that are even. The last step is to use the reduce function to sum all of those items together to get the final result.

I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Updated**
Here is my approach to Problem 3 in Clojure.

–Proctor

Project Euler in Clojure – Problem 1

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.

Problem 1 of Project Euler is described as:

Add all the natural numbers below one thousand that are multiples of 3 or 5.

The solution I came up with is:

(defn problem1 []
  (reduce + (filter #(or (zero? (rem % 3)) (zero? (rem % 5))) (range 1000))))

I take the range of all numbers from 1 to 999, filter out those numbers evenly divisible by 3 or 5, and then reduce/apply the addition operator to that sequence of numbers.

I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Updated**
Here is my approach to Problem 2 in Clojure.

–Proctor

XMLisp?

I had a twisted thought about a potential future thought experiment of using XML and Lisp style languages.

Having used Lisp a very little bit back in college for one semester, and read more about it in Structure and Interpretation of Computer Programs, I started looking into Clojure recently. I did a session of CodeRetreat last year in it, and was hearing more about it this year at SCNA so I started to read up on it more and play a little bit with the language.

Tie that in with that I recently was transferred to a new group at work that is doing some SOA (Service Oriented Architecture) work. Something triggered when I thought about the XML payloads being sent between the SOA Web Services and how that tied into what I am reading about Lisp and Clojure.

In other languages we think about serializing command objects into XML and back and send those messages between Services as a message payload. What made me think was that XML is a tree structure as well as the code in a Lisp type language.

What if we did something like Javascript and JSON? What if we convert the Lisp structure to XML and back, and then we can execute this Lisp structure data as Lisp code? With XML we can then also apply transforms and convert one message/command into another message/command, which would allow one message to be sent and transformed into multiple messages to be received by inserting messaging splitters and transformers. This is also not worrying about things like the security of the evaluation of the Lisp data as code since this something to think about as a thought experiment.

I don’t know if this is a novel idea, or if someone else has already tried it, but to me it seems like an interesting thing to think about and mull over.

Global Day of Code Retreat thought dump

Last Saturday, December the 3rd, was host the Global Day of Code Retreat. There were 90 cities participating with over 2000 developers taking time to practice and hone their skills. I was lucky enough to be a co-organizer for the one in Dallas, and wanted to share my thoughts about what I saw that day. For those unfamiliar with what a Code Retreat is I urge you to go check out the Code Retreat site for more information.

There were a number of things that impressed and surprised me about last weekend. First I was impressed by how many people showed up to the Dallas Code Retreat, we had nearly 40 people show up, and this was with only about a two week notice for the event. To have that many people set aside a day, to come out and practice coding with less than a two week notice was highly impressive. I was also impressed by the diversity of the people that showed up, we had people of all levels show up and participate, as well as a good diversity of people from those who had never pair programmed or heard of TDD to those who pair program or TDD on a daily, or near daily basis.

As one of the co-coordinators, and having has participated in two previously, both after the SNCA Conference the last two years, I only paired in one session. I was more interested in letting everyone else have an opportunity to experience a Code Retreat in the hopes of getting this to be something we can have happen regularly in the Dallas/Fort Worth Metroplex.

As I wasn’t the facilitator, we had Glenn Vanderburg, to whom I offer a hearty thank you to yet again, but as the odd man out I was able to walk around and see what the other pairs were doing. It was interesting to compare what the other pairs were doing compared to some of the approaches me and my pairs had done in the two I have participated in. It was also interesting to see how expressive the code they were writing was depending on the interval of when I swung by.

Though on my one pairing session, which was in Ruby, I noticed something interesting. I am not sure if it was the tests and the order they were written, or if it is some of the elegance/syntactic sugar of Ruby itself that lead to the route taken. And after thinking about it, I am not sure that I like the end result, which I will express after I show what it was.

We were testing an Alive state object and the transition between Alive and Dead. The tests started with zero alive neighbors returning a Dead state object.

def transition(alive_neighbors_count)
  Dead.new
end

The next test we wrote was for one alive neighbor. Which passed as expected.

We then tested for two alive neighbors, and that it should transition to an Alive state.

def transition(alive_neighbors_count)
  return self if number_of_alive_neighbors == 2
  Dead.new
end

The next test was for three alive neighbors, and that the state should transition to an Alive state as well. The code then looked like the following.

def transition(alive_neighbors_count)
  return self if (2..3).include?(alive_neighbors_count)
  Dead.new
end

While the range object and the include? method is expressive compared to an if statement about the number being between 2 and 3, after I thought about it for a while, think it is the wrong type of expressive. We opted for the expressiveness in terms of being in a range instead of the expressiveness of the domain and the actual ruleset. Conway’s Game of Life talks about dying as if due to underpopulation and dying as if due to overpopulation, but nowhere did we wind up expressing this in the code, unlike the following which expresses that domain knowledge.

def transition(alive_neighbors_count)
  return Dead.new if underpopulated?(alive_neighbors_count)
  return Dead.new if overpopulated?(alive_neighbors_count)
  self
end

This is more intended as an example of the following food for thought: Is the expressivity of the language leading you away from coming up with a way of expressing the domain in the language and losing the expressivity of the domain?

How Goodreads.com Changed My Reading Habits

“But you don’t have to take my word for it.”
–LeVar Burton

This past year I wound up coming across Goodreads and have been very impressed with what I have seen so far. Before finding Goodreads, I had been looking for a bookshelf system, as I have historically been bad about tracking what I read. While I can remember that I read something usually, I am bad about gauging when I have read it. It had been on my things to do, but it became reinforced after reading Apprenticeship Patterns by Dave Hoover and Adewale Oshineye.

Goodreads helps solve this problem for me. I can go mark books as being read, and then when I complete it, I move it to my “Read” shelf, and it sets the date of completion for me. I have also noticed that it helps me focus on finishing books more as well. I have the occasional habit of picking up and starting other books while I am reading one, and have even had three or four books going at the same time. These are not just reference style books as well, but different topics on different subjects some times. By me seeing a notice on my shelf that I am reading this, it helps to remind me to come back and finish reading the book, especially if it is a reference book, that is not setup that it is expected that it will read all of the way through.

The other reason that I had been looking for a good bookshelf tracking software, is that the way I was tracking books that I wanted to read, was by using a folder of bookmarks in my web-browser. Now I primarily use Chrome as my browser of choice, so I could see a list of books across a couple of different computers as I enabled the synchronization feature in Chrome, but to go through and find books was getting so that I could not even see the whole list when I was viewing it on my 30″ monitor. Goodreads helped to give me a good way to manage that list. They even have an iPhone app, so I can manage my “To-Read” shelf even when I am away from the computer.

The other feature I am loving about it that helps make it amazing in my book is the social aspect of it. I can add people I know as friends, and I can see their updates in a news stream. I have it setup so that I can see when one of my Goodreads friends adds a book as something they want to read, what progress they are making, and when they rate books. This has lead me to find a whole number of books that I likely would not have encountered normally.

The social aspect has a secondary benefit in that I can start to find out how my friends tastes in books correlates to mine. Not only can I see their ratings show in my news feed, so I can start to notice if our interest in topics align, but I can also browse their bookshelves and look at their history of books they have marked as reading, and go dig for new book ideas to add to my “To-Read” list. Goodreads aslo has a feature that lets me run a comparison of our tastes, and see how much of our lists overlap, and how we rank within that list.

I highly recommend this to anybody looking for a good bookshelf tracking site, as I am sure I have only scratched the surface of the features they provide, but am loving it and recommending it to anybody when they mention they are reading a book. If you are interested in checking out my profile or following me on Goodreads, my profile can be found at: http://www.goodreads.com/stevenproctor.

Happy Reading, and

“I’ll see you next time.”
–LeVar Burton

Agile Testing with Lisa Crispin – Part 2

Here is Part 2 of my notes on Lisa Crispin’s talk on Agile Testing. If you haven’t go catch up on Part One.

Lisa noted that her team stopped committing at the sprint level. They just work hard, don’t waste time and just focus on delivering the software. This works by letting the customer know that you are working by being transparent to them.

Teams need time to learn, experiment, and need slack. Need to give the team time to innovate and catch up on the latest technology, as well as to have time to move to the latest technology.

Automated tests need as much care and feeding as the code.

She noted that by learning the business it helped cut down time dealing with production support. She noted that they found scenarios where they could automated support tasks, or were even solving the wrong business problems. Lisa gave an example where a user kept requesting a report and it was being delivered to the user as they understood it, but it took sitting down with the end user to actually understand the report as the user was actually requesting it.

Lisa made reference to look at: Daniel Pink and Intrinsic Motivators, The Agile Samurai by Jonathan Rasmusson, and Jim Heismith and Israel Gat and their research into measuring technical debt, and her article Selling Agile to the CFO.

The quote of the evening seemed to be: “If it doesn’t have to work, you don’t have to test it.”

Emphasized that QA shouldn’t be treated as separate from development; QA time is part of development time.

Lisa pointed out that the most value was not in the actual integration tests, but was in the communication between the developers and testers that resulted from the interaction.

If you have too many thing going on at the same time, you task switch too much, the result is that you have a hard time predicting when you are done.

Encouraged us to try to get away from labels and just try to deliver the best value and best quality software that you can.

Encourage cross pollination across different teams in the area. You never know where new ideas come from. She talked about how she brought back the idea of an impediment backlog from when she visited over in the UK. When she took this idea back to her team she noticed that just making the impediment visible helped the team address those issues. –This reminded me of the Craftsman Swap that both Obtiva and 8th Light encourage, as well as Corey Haines’ journeyman tour.