#!/usr/bin/perl
use 5.016;
use warnings;
use utf8;
binmode STDOUT => ':encoding(utf8)';
my @digits = (
["", "いち", "", "", "", "", "", "", "", ""],
["", "", "に", "さん", "よん", "ご", "ろく", "なな", "はち", "きゅう"],
["", "", "", "", "", "", "ろっ", "", "はっ", ""],
["", "", "", "", "", "", "", "", "はっ", ""],
);
my @figures1 = (
["", "", "", "", "", "", "", "", "", ""],
["", "じゅう", "", "", "", "", "", "", "", ""],
["", "ひゃく", "", "びゃく", "", "", "ぴゃく", "", "ぴゃく", ""],
["", "せん", "", "ぜん", "", "", "", "", "", ""],
);
my @figures2 = ("", "まん", "おく", "ちょう", "けい", "がい");
sub _f2 { $_[0] % 4 == 0 ? $figures2[$_[0] / 4] : "" }
sub _f1 { sub{ $_[0]->[$_[1]] || $_[0]->[1] }->($figures1[$_[0] % 4], $_[1]) }
sub _d { $digits[$_[0] % 4]->[$_[1]] || $digits[1]->[$_[1]] }
sub f {
map{ _f2
(@{$_}), _f1
(@{$_}), _d
(@{$_}) } (0 .. $#d)
}
foreach(
0, 1, 2, 3, 8, 9, 10, 98, 99, 100, 101, 110, 111, 1100, 11000, 110000,
300, 500, 600, 800, 900, 3000, 6000, 8000, 9000, 512, 123456789
){
say $_, " => ", f($_);
}
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwp1c2UgdXRmODsKYmlubW9kZSBTVERPVVQgPT4gJzplbmNvZGluZyh1dGY4KSc7CgpteSBAZGlnaXRzID0gKAoJWyIiLCAi44GE44GhIiwgIiIsICIiLCAiIiwgIiIsICIiLCAiIiwgIiIsICIiXSwKCVsiIiwgIiIsICLjgasiLCAi44GV44KTIiwgIuOCiOOCkyIsICLjgZQiLCAi44KN44GPIiwgIuOBquOBqiIsICLjga/jgaEiLCAi44GN44KF44GGIl0sCglbIiIsICIiLCAiIiwgIiIsICIiLCAiIiwgIuOCjeOBoyIsICIiLCAi44Gv44GjIiwgIiJdLAoJWyIiLCAiI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