2 Solutions To Project Euler Problem #1

In an effort to not go a whole month without blogging, and in the interest of posting some code samples, I give you two solutions to Project Euler: Problem #1. If you’ve never heard of it, Project Euler (pronounced “oiler” after the Swiss mathematician Leonhard Paul Euler) is a set of increasingly difficult programming challenges. Participants can write their programs in any language and the only goal is to solve the problems and learn something. There are no prizes and you don’t have to show your work.

I had looked at this project years ago, and I swear I thought I had already solved some of them, but maybe I only thought about doing it. Anyway, I have two solutions to the first problem; one in Groovy and the other in Scala. Here, then, is how Project Euler describes the problem

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.

So, the goal is to take a series of numbers from 0 to 1000, exclusive, find all the numbers divisible by 3 or 5 and add them together. Here’s the Groovy solution.

def subList = (0..<1000).findAll {it % 3 == 0 || it % 5 == 0} 

def sum = subList.inject(0) {i, sum -> sum + i}

println "Sum = ${sum}"

This is a very simple program, and I could have written it as a one-line program. I broke it up into a few lines for clarity. As you can see, the first line creates an exclusive range from 0 to 1000. It then calls the findAll method on that range, passing in a closure that will return true if the passed-in digit from the range, called “it” here, is evenly divisible by 3 or 5. The result of findAll is another collection, containing only those values that passed the divisibility test. We then take that list, passing 0 into the inject method, which will neatly sum the values up and return that value. Easy peasy.

Now here’s the Scala version. You’ll notice it is very similar to the Groovy solution.

val subList = for {
    i <- List.range(0, 1000)
    if i % 3 == 0 || i % 5 == 0
} yield i 

val sum = subList.foldLeft(0) {(i, j) => i + j}

println("Sum = " + sum)

I used a sequence comprehension to generate the sublist here. The bit beginning with “for” generates an exclusive range from 0 to 1000, which is then iterated over, assigning each value to “i”. Then, if it is divisible by 3 or 5, it is yielded up by the comprehension. The result is a collection of just those numbers that we want, assigned to the val called subList. We then call foldLeft on that sublist, doing exactly what we did in the Groovy solution. Again, pretty simple.

Now, I could have solved this one in an almost identical fashion to the Groovy solution by using the filter method of lists, but I wanted to solve it first using a list comprehension. Here is the second solution

val subList = List.range(0, 1000) filter {i => i % 3 == 0 || i % 5 == 0}

val sum = subList.foldLeft(0) {(i, j) => i + j}

println("Sum = " + sum)

I timed the solutions and all three finished in just over a second. The second Scala solution seemed ever-so-slightly faster than the other two.

As I get time, I will work on additional problems from the site and post the answers here. I don’t know that I’ll always do solutions in two languages, but I might.

My Varied Musical Tastes

I use the last.fm application on my Mac to “scrobble” what songs I’m listening to. This allows me to keep a record of what’s been playing, but it mostly allows me to be a complete exhibitionist and show “the world” what sort of music I like. When I indicate that I “love” a song, that even shows up on my Friendfeed page, for added coverage.

This morning, I happened to be on my last.fm page and I noticed the graph below, showing my top 15 artists by the number of songs I’ve played by them.

top15

Pretty interesting, eh? I’ve always had diverse musical tastes, but Frank is still the man. If you go to my page, the little arrow icon indicates that you can listen to some of that artist’s work. Shame on them for not having anything by Eilen Jewell!

I’m Hosting A Windows 7 Launch Party

windows_7_graphicNo, don’t start checking your thermometers to see if Hell has frozen over. Believe it or not, I’m actually hosting a Windows 7 Launch Party. I know, I know. I’ve become quite a Mac Bigotโ„ข over the years, and have not had anything good to say about Windows for a very long time. But things are different now.

I’ve been using Windows 7 on my netbook for a couple of months now, and it works wonderfully well. My company gave us Windows 7 the day it went RTM, and my work laptop has been rock solid since I 86’d Vista and upgraded to Win7. And last Saturday night, my son’s 2006 vintage AMD machine stopped booting. After trying to repair whatever might have been wrong with the XP that was on it, I decided to see if Win7 would run on it. I installed it and it runs better now than it did before. And Thomas loves the way Win7 looks.

I like the fact that it doesn’t BSOD every 2 hours, like Vista on my work laptop did.

So, why am I doing this party thing? Well, when I first heard about it on TWiT, I laughed. A lot of people laughed. But then I thought about it a bit. For doing just a little bit of work, such as inviting friends who probably won’t come ;-), I get some neat collectible Win7 stuff, and a free copy of Windows 7 Ultimate “Signature Edition” which, apparently, means Steve Ballmer signed the box, or something like that. That seemed like a fair trade to me.

I have invited several people to the party, but most have not yet RSVP’d. So, I’m extending this invitation to people who know me in the real world, live somewhere near Atlanta, and are willing to drive out East to where I live. The “know me in the real world” bit is very important, as I won’t give my home address to some crazed maniac I only know through the aether. Crazed maniacs I know IRL are fine.

The party is Saturday, October 24 at 8:04 PM. I will have my netbook out if people want to look at it with Win7, and you can feel free to “ooh” and “ah” over it. If you’re interested in coming, email me.

Here’s the text of the invitation that I wrote for houseparty.com to send out in the invitations:

Hey Friend of Joey,

You’re receiving this because I’m throwing a Windows 7 Launch Party. Yes, you read that right: the Mac Bigot is hosting a Windows party. Why? Because I actually like Win7, and I thought it might be fun. (And I get a free copy of Windows 7 Ultimate…) If you come, we can stand around my ASUS Netbook or my Dell laptop, both of which have Win7 loaded, and ogle them and their awesomeness, for which there is no charge. There will be a modicum of food and libation, which means Cheetohs and Coke, most likely. ๐Ÿ˜‰ As part of the “party pack” we get Windows7 plates and napkins and such, which will only enhance the joy!

All kidding aside, I hope you can come.

So, if that doesn’t make you want to come, I don’t know what will. ๐Ÿ™‚

So A Guy Comes To My Door, Wanting To Sell Me Some Meat…

As the title suggests, about 30 minutes ago a guy comes to the door, wanting to sell me some meat. That sounds like the setup for a bad joke, but it isn’t. My wife told him to wait outside, and then she came and got me. I went out on the porch and he proceeded to tell me that he was from “The Cattle Exchange” and that he had some great deals on “steaks, pork, chicken and seafood.” I walked up to his truck, and he proceeded to show me a box of nice-looking, vacuum-packaged steaks. After showing me all the meat, he whipped out a price list. Here’s a recap of the ensuing discussion

Him: Now, if you were to order this online, it would cost you $451. But when I’m out here, doing what I do, I’m supposed to get $299…

Me: Wow. That’s still a lot of money.

Him: … and if you buy the case, I’ll throw in a case of chicken for $20.

Me: That’s still an awful lot of money for meat.

Him: Well, what if I cut another $100 off that price?

Me: You know, I’m gonna pass. We just don’t eat that much steak. Plus, that’s still a lot of money, to a company I’ve never heard of, and a guy who showed up at my door wanting to sell me some meat. No offense. (That last was said in my best Paulie Walnuts voice, with the appropriate hand gestures.) I’ll look your company up online and maybe think about it.

He then packed up his wares and left.

The whole thing just felt wrong. My first thought was that either this guy is ripping off his company, and selling it for less than he was supposed to, or his company was ripping people off on their website. I came back inside and looked up the company. It turns out my Spidey Senseโ„ข was spot on with this outfit. Check out this headline Atlanta Cattle Exchange Complaints – Door-to-door meat sellers rip off! The comments are pretty enlightening, too. Looks like I made the right decision in not trusting him. It’s basically a scam from start to finish.

So, if a guy shows up at your door trying to sell you some meat, you should… wait for it… send him packing. Get it? Get it? Meat… packing… Thank you, thank you. I’ll be here all week. Try the veal and remember to tip your waitress.

iPhone Interface For My Blog

09/28/2009 Update: Now added a link to the view from Android.

Last week I learned about WPTouch, which is a plugin for WordPress that reformats the theme for the iPhone, Android and other mobile devices. It was an easy install, and I am now happy to report that if you view my blog on a mobile device, you’ll see the new UI. Here’s what it looks like on an iPhone

IMG_0465

If anybody has an Android phone, or some other supported mobile device, send me a screenshot so I can see what it looks like.

Thanks to Steve Ziegler, here’s what it looks like from an Android device. Thanks, Steve!