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

mu chance or much chance ?

日々の戯れ言

Project Euler 15

Project Euler

Problem15を解きました.

  • Lattice paths

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?

https://projecteuler.net/problem=15

問題は「20×20のマス目の左上からスタートした場合,引き返しなしで,右下にいくルートの総数を求めよ」.

def fact(num)
  mul = 1
  for i in 1..num do
    mul *= i
  end
  mul
end

p fact(40) / (fact(20) * fact(20))