Guelph Coffee and Code + Project Euler

I’m talking about Project Euler at tonight’s Guelph Coffee and Code. My talk is going to be short, but here’s the main talking points.

Lenhard Euler: Mathematician

Euler (pronounced “Canada’s Worst Hockey Team that’s not The Senators”) was a Mathematician in the 1700s. He was a genius, and he helped shape the world of mathematics that we know and love today. Check out Euler on Wikipedia for more in depth info about the fellow.

What’s this all about?

Project Euler is gets you to combine mathematical insights with computer programming in an effort to find answers to a series of over 300 problems. It’s not tied to a particular language, though there are some languages that will serve you better than others. It’s all about finding the answer in an elegant fashion.

Why should I care?

Project Euler will help you to identify areas of interest for you, and to make you a better all around programmer (and mathematician). There’s a ton of different subjects that are covered, and it’s an opportunity to push your self in new directions.

Let’s do an example!

Sure thing! Look at Question #1.

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

Not hard right? Make a loop that goes from 1 to 1000, and for each number check if it’s divisible by 3 or divisible by 5. If it is, add it to a running sum. Pseudocode looks something like this:

x=1
sum=0
while x is under 1001
    if x is a multiple of 3 then sum = sum + x
    if x is a multiple of 5 then sum = sum + x

And at the end return sum. Does that give the right answer? Are we forgetting something?

Of course, if x is a multiple of both 3 and 5, then we’ve counted it twice. We can fix that in a few ways, like subtracting off x one time if it’s a multiple of 3 and 5.

if x is a multiple of 3 and 5
then sum = sum - x

Or we could just solve it with an else

if x is a multiple of 3 then sum = sum + xM
else if x is a multiple of 3 then sum = sum + x

Now a lot of people will understand that the “trick” is not really tricky in this case. Remember not to count multiples of 3 and 5 twice is pretty simple. But the curve is pretty steep – after 20 or 30 questions, it’s much more hidden.

And that’s the gist of what I’m saying tonight.

By the way, here’s that pseudocode in python:

f=0
x=1
while x < 1000:
    if x%3==0:
        f=f+x
    elif x%5==0:
        f=f+x
    x=x+1
print f

Exponential Growth

According to the CBC G8/G20 costs have grown exponentially. I can only infer that if the first 3 days cost $1,000,000,000, then the next 3 days will cost $1,000,000,000,000,000,000 (the cost squared, typically the smallest of what is considered exponential growth). That’s unfortunate, because I think that’s more money than the entire world has, so we’re probably all going to starve to death now.

Project Euler

I’m a latecomer to the Project Euler game, but I’m looking for a strong finish.

I’ve been trying to hone my Python skills, so I Dove Into Python with a vengeance. That didn’t quite sate my desire for crazy coding fun, and my good buddy Sean suggested the Infamous FizzBuzz test.

Well, I loved it, and I searched for more similar problems, and I got to the first Project Euler question:

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.

I was thrilled! A minor modification to my fizzbuzz program and I was off and running. I’ve currently completed somewhere around 20… but I’ll admit that I’ve skipped to the end am trying my hand at Problem 282, the dreaded Ackermann Function.

So – if you’re a coder, why don’t you try it out?

If a picture’s worth 1000 words, then why can’t I eat you?

We have often heard the old saw, “A picture is worth a thousand words.” Is that really true? Here are a few points to consider.

We see at about 22 – 24 fps. Each individual frame that we see can be considered to be a picture. If a picture is worth a thousand words, then we’re taking in 22,000 to 24,000 words per second.

  • A movie is something you watch, and almost flawless (for the purpose of being seen, not from any artistic point of view).
  • A movie is usually about 100 minutes (or so) long.

From this, a moving picture is worth about 22 to 24 thousand words per second. A full length motion picture or movie is worth between 132 and 144 million words.

To put that into perspective, “Moby Dick” had about 208,000 words. “Atlas Shrugged” is about 645,000 words.

So, “Atlas Shrugged” would be worth about 10 minutes worth of a movie, using this formula.

I think that’s a little bogus. Let’s look at it this way:

“Moby Dick” would be a pretty decent 2.5 hour movie (150 minutes). (Editor’s Note – I realize that there are a few Moby Dick movies – 22, 77, 116 and 180 minute versions. If I was making it, I would aim for 2.5 hours. That’s what I meant. Plus, if I picked a different time, then it wouldn’t prove my point!) That’s about 1385 words per minute, or approximately 23 words per second. Funny thing – didn’t we say that a moving picture has 22 to 24 frames per second?

So, apparently, a picture is worth one word.

QED
(Editor’s Note: QED is an acronym that one puts at the end of a math problem. It stands for Pancho Villa saying, “Questeeon Ees Done.” I considered posting this in a new category called “Lunacy” or possibly “Frivolity.”)