232792560

I recently stumbled on Project Euler, a series of various mathematical/programming challenges. A lot of them can be solved quite easily in Perl, such a problem 5:

What is the smallest number divisible by each of the numbers 1 to 20?

I’m sure there’s a more elegant solution, but I figured, heck that’d be easy to brute force. So I whipped up a few lines of Perl. Here’s my first pass:


#!/usr/bin/perl -w

use strict;

my $value = 0;
my @divsors = reverse(2 .. 20);

EULER: while (1) {
$value += 20;
print “$value…\n” unless ($value % 1000000);

for my $i (@divsors) {
next EULER if ($value % $i);
}

last;
}

Anything like while(1) always makes me worry about an infinite loop, so I threw in the status line & watched it.

Happily, there was no infinite loop & it terminated.

0.0001 seconds later I realized what an idiot I was.

The programs goes through all the hard work of figuring out the value and then… well, nothing.

I forgot to add a line to print it out the final value.

So, in a nutshell, my first attempt worked — all the heavy lifting bits were correct — but it was worth naught as I overlooked a trivial bit.

Sadly, this is apt microcosm of my life.

One Response to “232792560”

  1. Christopher Smith Says:

    So, you did miss the easy cheesy solution that you can do in 30 seconds with a calculator. It helps to think about prime factorization when looking at the problem.

    Anyway, enjoy it. Project Euler is a lot of fun.

Leave a Reply