Sunday, November 22, 2009

Euler #4

draegtun had a nice post on Project Euler #4. The goal is to find the largest palindromic product of two three digit numbers, and he lists solutions in Clojure and Python and a bunch in Perl 5. There is also a Perl 6 version by trenton in euler_bench on github.

I thought I'd try my hand at a more idiomatic Perl 6 version. Here's my first attempt:
((100..999) X (100..999)).map({$^a * $^b}).grep({$_ eq $_.flip}).max.say

Unfortunately, the usual caveats apply. This blows up the current Rakudo, no doubt because without lazy lists it tries to construct a huge list entirely in memory and then collapses. Rakudo ng has lazy lists, but it doesn't have grep or max yet. So for now, this is purely a hypothetical implementation.

However, with any luck, Rakudo ng will handle this by the end of the week...

2 comments:

  1. ng is also lacking X and flip, on further inspection. But it does have a first stab at map and say.

    ReplyDelete
  2. Sweet, glad to hear about rakudo progress and I'm very glad that euler_bench has been helpful. Though to give credit where credit is due, it looks like @leto's work (http://github.com/notbenh/euler_bench/commits/master/perl6/004/01.pl).

    ReplyDelete