加泰罗尼亚语用java代码编号 [英] Catalan number with java code

查看:66
本文介绍了加泰罗尼亚语用java代码编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我返回加泰罗尼亚语号码的代码,它给出了正确的答案,直到num = 6,而对于num> 6,答案返回错误,请我弄清楚原因。


我的尝试:



this is my code for returning a catalan number, it gives me the correct answer until the when num=6 and for num>6 the answers return are wrong, please i cant figure out why.

What I have tried:

return (isFactorial(2 * num)) /((isFactorial(num + 1)) * (isFactorial(num)));

推荐答案

你应该使用一个好的阶乘函数( isFactorial 名称看似可疑)以便应用这样的公式。

请注意,在加泰罗尼亚语的维基百科页 [ ^ ],您可以使用递归关系找到更好的公式来计算它们。试试,例如

You should use a good factorial function (isFactorial name looks suspicious) in order to apply such a formula.
Please note, on the very Wikipedia page of Catalan numbers[^], you may find better formulas, using recurrence relations, to compute them. Try, for instance
class Catalan
{
  static int catalan(int n)
  {
    int c = 1;
    for (int i = 0; i <n; ++i)
    {
      c = 2 * c * (2 *i + 1) / ( i + 2);
    }
    return c;
  }
  public static void main(String arg[])
  {
  for (int n=0; n<10; ++n)
    System.out.printf("catalan(%d) = %d\n", n, catalan(n));
  }
}


这篇关于加泰罗尼亚语用java代码编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆