Java: Calculating binomial coefficient

后端 未结 3 1813
渐次进展
渐次进展 2021-01-07 11:30

I have the following programm calculating the binomial coefficient of two integers. But I want to change the programm, that it calculates and saves only the necessary coeffi

3条回答
  •  鱼传尺愫
    2021-01-07 12:17

    Do you want to keep your code afterall? Because you can also compute the binominal coefficient recursively, which would reduce your function to these 4 lines:

    static long binomi(int n, int k) {
            if ((n == k) || (k == 0))
                return 1;
            else
                return binomi(n - 1, k) + binomi(n - 1, k - 1);
        }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题