読者です 読者をやめる 読者になる 読者になる

初代Masteries

きっとモヒカンにもなれないお前たちに告げる!!!

Project Euler - Problem 17

※先週は就活で忙しかった為, 1週間お休みさせて頂きました. ご了承下さい.

Problem 17

1から1000までの数字を英単語で書いた時, 全部で何文字になるかを求める問題.
とりあえず答えは出たけれど, もっとスマートに書けそう...

use strict;
use warnings;

my $number = {
    0 => '',
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
    4 => 'Four',
    5 => 'Five',
    6 => 'Six',
    7 => 'Seven',
    8 => 'Eight',
    9 => 'Nine',
    10 => 'Ten',
    11 => 'Eleven',
    12 => 'Twelve',
    13 => 'Thirteen',
    14 => 'Fourteen',
    15 => 'Fifteen',
    16 => 'Sixteen',
    17 => 'Seventeen',
    18 => 'Eighteen',
    19 => 'Nineteen',
    20 => 'Twenty',
    30 => 'Thirty',
    40 => 'Forty',
    50 => 'Fifty',
    60 => 'Sixty',
    70 => 'Seventy',
    80 => 'Eighty',
    90 => 'Ninety',
    100 => 'Hundred',
    1000 => 'Thousand',
};

my $total = 0;
$total += length num2word($_) for (1..1000);
print "$total\n";

sub num2word {
    my $n = shift;

    # 1桁の場合
    return $number->{$n} if length $n == 1;

    my $word;
    my @nums = reverse split //, $n;

    # 4桁目計算
    if (defined $nums[3]) {
        $word .= $number->{$nums[3]} . $number->{1000};
    }

    # 3桁目計算
    my $under2 = $nums[1] . $nums[0];
    if (defined $nums[2] && $nums[2] != 0) {
        $word .= $number->{$nums[2]} . $number->{100};
        $word .= 'And' if $under2 ne '00';
    }

    # 下位2桁計算
    if ($under2 ne '00') {
        if(defined $number->{$under2}) {
            $word .= $number->{$under2};
        } else {
            $word .= $number->{$nums[1].'0'} if $nums[1] != 0;
            $word .= $number->{$nums[0]};
        }
    }

    return $word;
}