Project Euler — problem 15

[This article was first published on Tony's bubble universe » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The 15th problem in Project Euler.

Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20×20 grid?

Mmm… walk in the grid; it sounds like a problem of graph theory. I’m sure there must be some complicated solution for this. But I’m gonna solve it with the simplest way. To define “pass by one grid in either direction” as one step. Without going back, one needs to 20 steps leftwards and 20 steps downwards to travel from the top left corner to the down right corner.  The problem turns to “how many combinations of leftwards steps with downwards steps”, which is a simple combination problem in mathematics. Thus, the answer is C(20, 40) .

?View Code RSPLUS
1
2
result <- choose(40, 20)
cat("The result is:", result, "\n")

To leave a comment for the author, please follow the link and comment on their blog: Tony's bubble universe » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.