スキップしてメイン コンテンツに移動

Project Euler - Problem 20

問題

  • 原文

    Find the sum of the digits in the number 100!

  • 日本語訳

    100! の各桁の数字の合計を求めよ。

解答

毎度のことですがMath::BigIntのお世話になりました。

#!/usr/bin/perl

use strict;
use warnings;
use feature qw/say/;
use List::Util qw/sum reduce/;
use Math::BigInt;

sub factorial(_) {
  no warnings qw/once/;
  my $n = shift;
  return 1 if $n == 0;
  reduce { Math::BigInt->new($a) * Math::BigInt->new($b) } 1 .. $n;
}

say sum split //, factorial(Math::BigInt->new(100));

コメント