Skip to content

Eulergy

November 30, 2009
tags:

It wasn’t my plan to get into all things Euler when I started this thread two posts ago.

However it has been a nice little tangent and doing a Google on Perl+Euler does throw up a number of links (over 100K of them in fact). Its worth following up some of these and playing around with the problem and the published code.

One such link is the blog post Distrust Simplicity: PROJECT EULER, PROBLEM #1. This post shows solutions to Euler problem 1 in Ruby, Python, Common Lisp, Clojure, Haskell and Perl.

Here is the Perl solution provided in said post:

#!/usr/bin/perl
use strict;
use List::Util qw(reduce);

# find the natural numbers less than 1000 divisible by 3 or 5
my @multiples = ();
foreach (1..999) {
    if ($_ % 3 == 0 || $_ % 5 == 0) { push(@multiples, $_); }
}

# sum them
print reduce { $a + $b } @multiples;

This could be simplified with a functional twist like most of the other solutions provided:

use Modern::Perl;
use List::Util qw(sum);

say sum grep { $_ % 3 == 0 || $_ % 5 == 0 } 1..999;

And lets not forget our mandatory autobox variation!

use Modern::Perl;
use autobox::Core;

[1..999]->grep( sub{ $_ % 3 == 0 || $_ % 5 == 0 } )->sum->say;

 
Another link I noticed which may bear fruit in the future is a very new Github repo for Project Euler solutions in Modern Perl.

To quote the Readme:

This module is intended to provide solutions to the Project Euler problems (http://projecteuler.net) using modern perl best practices and object oriented programming. For this reason I will be making heavy use of the Moose module(s) to wrap the solutions into objects.

I wish the author, Lespea (Adam Lesperance), the best of luck with this spanking new project.

I think thats enough Eulergizing from me for a while ;-)

/I3az/

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: