The first problem within Project Euler isn’t meant to twist the brain in knots and is a gentle introduction to what is coming further down the road. The problem states:
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.
As I mentioned in my initial post about exploring Project Euler, I noted that I often write in a lowest common denominator with whatever language I’m using at the time. The net effect of that decision is often a working but poorly optimised solution or a solution that doesn’t fit well with the spirit of the programming language.
The solution to problem #1 took three distinct evolutions, the first of which is below and looks like a solution you’d likely see in any programming language:
i = 1
sum = 0
while i < 1000:
if i % 3 == 0 or i % 5 == 0:
sum += i
i += 1
print sum
I wasn’t a fan of needing to declare the variables to start with for such a simple problem, so removed the need for the i but using the Python range function to generate a series of numbers. The range function allows you to generate any length series of numbers, starting and ending where you want with any stepping. By default, it starts at 0 and a stepping of 1 – which means range(1000) is going to generate a list of numbers from 0 – 999.
sum = 0
for x in range(1000):
if x % 3 == 0 or x % 5 == 0:
sum += x
print sum
Python provides functionality for its lists called list comprehensions. The idea behind a list comprehension is that you can perform the same action, whatever that may be, against each item in the set. In the final solution below, I have applied a filter (the if statements), which now only returns those items from the for x in range that match and I’m not applying an expression to the resulting matches (the solitary x before the for). Once collected the new list is passed to the sum function to yield our result.
sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])
Wikirank, Visualising Wikipedia Usage Data
I came across a clever web site named Wikirank, which provides visualisation tools to explore and compare the usage data from wikipedia.org.
If you’re wondering how Wikirank could manage that, wikipedia.org provide access to their web server traffic logs as a service to the community for free. Wikirank consumes that public data, analyses it and provides a convenient way to see what topics on wikipedia.org are popular at the moment.
Wikirank isn’t just a tool to find out what is popular at the moment though, it also lets you view the usage data on a nominated page over time, up to the last 90 days. That sort of functionality is great, as it lets you see how a particular topic is being received among the community. Not wanting to stop there though, Wikirank also lets you compare different topics as well. The example on the Wikirank home page at the moment is who is more popular out of John, Paul, George or Ringo from The Beatles and according to Wikirank, John Lennon is nearly twice as popular as Paul McCartney.
I think Wikirank is going to be a fantastic companion to the primary wikipedia.org web site. It’d be facinating if they spun off a wikianalytics.com and broke down the usage data from wikipedia.org and allowed people to explore that data in a similar but cutdown fashion to what Google Analtyics provides.