美文网首页
和果子一起来做题-Project Euler-15-R语言版本

和果子一起来做题-Project Euler-15-R语言版本

作者: 9d760c7ce737 | 来源:发表于2018-01-28 18:23 被阅读17次

    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.


    mark

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

    把向右看成0,向下看成1,那么问题转化成40个位置中,放入20个1,有多少种可能?

    就是从40中选20的排列组合
    在R语言中,组合数的计算:从 N 中选择 K 个用函数 choose(N,K)

    > choose(40,20)
    [1] 137846528820
    

    讲到组合就要提起阶乘,R语言的阶乘的计算至少有三种方法:
    1.prod(1:N):这个在之前已经介绍过
    2.factorial(N):

    > factorial(4)
    [1] 24
    > factorial(3)
    [1] 6
    > factorial(10)
    [1] 3628800
    

    3.gamma(N+1): 不建议使用,理解起来容易混淆,gamma(4)实际上是3的阶乘

    > gamma(4)
    [1] 6
    > gamma(5)
    [1] 24
    > gamma(3)
    [1] 2
    

    已经有了choose函数可以做排列组合,我们还可以自己写一个

    相关文章

      网友评论

          本文标题:和果子一起来做题-Project Euler-15-R语言版本

          本文链接:https://www.haomeiwen.com/subject/txezaxtx.html