Windows PowerShellでProject Euler(27)

Problem 20

当然ですが、掛けていくだけですね。数字を配列にすれば最後も簡単です。

function fold($f, $x0) {
    begin   { $x = $x0 }
    process { $x = &$f $x $_ }
    end     { $x }
}

function mul($x, $y) {
    $c = 0
    foreach($e in $x) {
        $n = $e * $y + $c
        $r = $n % 10
        $r
        $c = ($n - $r) / 10
    }
    
    while($c -gt 0) {
        $r = $c % 10
        $r
        $c = ($c - $r) / 10
    }
}

function factorial($n) {
    1..$n | fold { mul $args[0] $args[1] } @(1)
}

(factorial 100 | measure -sum).sum