java 谐波序列递归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12904284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:43:40  来源:igfitidea点击:

Harmonic sequence recursion

javarecursion

提问by vaindil

I'm really getting the hang of recursion (or so I think), but this problem is tripping me up. I'm trying to return 1 + 1/2 + 1/3 + ... + 1/n, but no matter what I try the method returns 1.0. I cannot for the life of me figure out what's wrong.

我真的掌握了递归的窍门(或者我认为是这样),但是这个问题让我很困惑。我试图返回 1 + 1/2 + 1/3 + ... + 1/n,但无论我尝试什么,该方法都会返回 1.0。我一生都无法弄清楚出了什么问题。

public static double harmonic(int n) {
    if(n == 1) {
        return 1;
    } else {
        return (1 / n) + (1 / harmonic(n - 1));
    }
}

采纳答案by Brian

Well, for one, you don't want to return (1 / n) + (1 / harmonic(n - 1)), but also you need to use doublearithmetic:

嗯,一方面,你不想 return (1 / n) + (1 / harmonic(n - 1)),但你也需要使用double算术:

public static double harmonic(int n) {
    if(n == 1) {
        return 1.0;
    } else {
        return (1.0 / n) + harmonic(n - 1);
    }
}

If you left it as 1 / harmonicyou'd return another function entirely:

如果你离开它,1 / harmonic你会完全返回另一个函数:

(1 / n) + 1 / ( 1 / (n - 1) + 1 / ( 1 / (n - 2) + 1 / (...) ) )

(1 / n) + 1 / ( 1 / (n - 1) + 1 / ( 1 / (n - 2) + 1 / (...) ) )

That is a very confusing function to figure out, btw, but I think (with my 3rd time editing it) I got it right this time.

顺便说一句,这是一个非常令人困惑的功能,但我认为(我第三次编辑它)这次我做对了。

回答by Claudiu

You want to use floating point division:

你想使用浮点除法:

public static double harmonic(int n) {
    if(n == 1.0) {
        return 1.0;
    } else {
        return (1.0 / n) + (1.0 / harmonic(n - 1.0));
    }
}

That is: 1/2is 0; 1/2.0is 0.5.

即:1/201/2.00.5

回答by Rohit Jain

Thats because integer division gives integer result.

那是因为整数除法给出整数结果。

So, 1/2 == 0

所以, 1/2 == 0

You can use rather use floating-pointdivision like this: -

您可以floating-point像这样使用除法: -

if(n == 1.0) {
    return 1.0;
} else {
    return (1.0 / n) + harmonic(n - 1); // Should be `harmonic(n - 1)`
}

回答by paddy

You need to use doubles. Right now, you're doing 1 / n, both of which are integers. Change it to:

你需要使用双打。现在,您正在执行1 / n,这两个都是整数。将其更改为:

return (1.0 / n) + (1.0 / harmonic(n - 1));

回答by FThompson

Use doubles in your division calculations. Currently, everything is cast to ints, losing any floating-point precision you would normally expect.

在除法计算中使用双精度数。目前,一切都被转换为整数,失去了您通常期望的任何浮点精度。

public static double harmonic(int n) {
    if (n == 1) {
        return 1;
    } else {
        return (1.0 / n) + (1.0 / harmonic(n - 1));
    }
}

回答by Seid.M

the recursion part should not include 1/harmonic(n-1)it should be

递归部分不应该包括1/harmonic(n-1)它应该是

   public static double harmonic(int n)
  {
    double harm = 0.0;
    if (n == 1) {
        return 1.0;
    }
    else {
        harm = harm + (1.0 / n) +  harmonic(n - 1);
    }
    return harm;

}

回答by Hrishikesh Mishra

/**
 * Created by hrishikesh.mishra on 04/01/16.
 *
 * Describe a recursive algorithm
 * for computing the nth Harmonic number,
 * defined as Hn = ∑ n k=1 1/k.
 *
 */
public class HarmonicNumber {


    public static void main(String[] args) {

        System.out.println("Sum up to 1: "  + sum(1));
        System.out.println("Sum up to 2: "  + sum(2));
        System.out.println("Sum up to 3: "  + sum(3));
        System.out.println("Sum up to 4: "  + sum(4));
    }


    /**
     * Summation with recursive method.
     * @param n
     * @return
     */
    public static double sum(int n){
        if(n <= 1)
            return 1;
        else
            return ((double) 1/n) + sum(n - 1);
    }
}

回答by Mahmoud

 public static double harmonic(int n)
   {
      if (n==1)
      return 1/n;
      else return 1/n + harmonic(n-1);
   }