查找分区,复杂的递推公式 [英] complex recursion formula for finding partitions

查看:172
本文介绍了查找分区,复杂的递推公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来计算使用下列公式的自然数分区。式生成两个正数,则两个负等等。你停下时,P(n)的< 0的一个例子是

  P(3)= P(2)+ P(1)= 3
  P(4)= P(3)+ P(2)= 3 + 2 = 5
  P(5)= P(4)+ P(3) - 对(0)= 5 + 3  -  1 = 7
  P(6)= P(5)+ P(4) - 对(1)= 7 + 5  -  1 = 11
  * P(0)= 1按照惯例
 

在为了计算换句话说说P(5),你将不得不计算的P(4),其等于P(3)+ P(2)和和P(3),其等于P(2)+ P( 1),最后 - P(0),它等于1。你将不得不向下遍历每个找到他们平等的,然后总结他们。因此,要找到一些分区,你必须找到所有其他号码的每个分区。我曾尝试一些东西,你可以看到下面的code,但它不工作。 K =计数器在我的code。

  code:
    公共静态长SerialFib(N久)
    {
        长指数为0;
        双前;
        柜长= 1;
        EX = Math.pow(-1,反 -  1);
        指数=(长)前;
        如果(正℃,)
        {
            返回0;
        }
        其他
        {

            返回SerialFib((指数*(N  - ((柜*((3 *计数器) -  1))/
            2))))+ SerialFib((指数*(N  - ((计数器*((3 *计数器)1))/ 2))));

        }
    }
 

解决方案

计数器将始终为1,因为你不是它传递回SerialFib。另外,你需要一个基本情况当n等于0时返回1。

第一基本情况:

 如果(N == 0)
   返回1;
 

SerialFib应该有另一个参数计数器:

  SerialFib(N久,INT计数器)
 

当你调用SerialFib它应该是这样的:

  SerialFib([公式],++柜);
 

I am trying to calculate partitions of a natural number using the formula below. The formula generates two positive numbers, then two negative and so on. You stop when P(n) < 0 An example is

  p(3) = p(2) + p(1) = 3
  p(4) = p(3) + p(2) = 3 + 2 = 5
  p(5) = p(4) + p(3) -  p(0) = 5 + 3 - 1 = 7
  p(6) = p(5)  + p(4) - p(1) = 7 + 5 - 1 = 11
  *P(0) = 1 by convention

In other words in order to calculate say P(5) you would have to calculate P(4) which equals P(3) + P(2) and and P(3) which equals P(2) + P(1) and finally - P(0) which equals 1. You would have to traverse down for each to find what they equal and then sum them. So to find a partition of a number you would have to find the partitions of all other numbers each. I have tried something as you can see the code below but it does not work. k = counter in my code.

    Code:
    public static long SerialFib( long n ) 
    { 
        long exponent = 0;
        double ex;
        long counter = 1;
        ex = Math.pow(-1, counter - 1);
        exponent = (long) ex;
        if (n < 0)
        {
            return 0;
        }
        else
        {

            return SerialFib((exponent * (n - ( (counter * ( (3 * counter) - 1)) / 
            2)))) + SerialFib((exponent * (n - ( (counter * ( (3 * counter) +1))/2))));

        }
    } 

解决方案

Counter is going to always be 1 because you aren't passing it back into SerialFib. Also, you need a base case for when n is equal to 0 it will return 1.

first base case:

if(n==0)
   return 1;

SerialFib should have another parameter for counter:

SerialFib(long n, int counter)

When you call SerialFib it should look like:

SerialFib( [your formula], ++counter);

这篇关于查找分区,复杂的递推公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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