Sunday, August 2, 2009

Project Euler #17

Euler Project #17: "If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

"If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?"

Their notes say something about "and" in numbers British-style, but I have ignored this as I have no idea where the ands would go.

This is a slow, stupid way to solve this problem. (Though I'm reasonably happy with it as an example of Perl 6 programming.) On the other hand, this way of doing it makes it much easier to check for correctness. (Though I discovered I'd misspelled one of the numbers in the process of posting this. Sigh.) This runs in just over 3 minutes on my MBP. I will be very disappointed if I can't get that time below 20 seconds. But that is a matter for a future post.

PS I would really like to use Yuval Kogman's system for cleaning up / speeding up Gist embedding in blog posts. Unfortunately, I can't quite make sense of what he is actually doing. Afraid I have fallen a bit behind the technological curve here...

2 comments:

  1. If using Perl 5, this problem just begs for some
    CPAN leverage:

    use 5.10.0;

    use Lingua::EN::Numbers qw/ num2en /;
    use List::Util qw/ sum /;

    say sum map { y/a-z// } map { num2en( $_ ) } 1..1000;

    :-)

    ReplyDelete
  2. Ha! Yes, as always, you can get rather drastic improvements by using CPAN.

    ReplyDelete