简体   繁体   English

递归 - Java

[英]Recursion - Java

I am working on a program where I have to use recursion to calculate the sum of 1/3 + 2/5 + 3/7 + 4/9 + ... + i / (2i + 1). 我正在开发一个程序,我必须使用递归来计算1/3 + 2/5 + 3/7 + 4/9 + ... + i /(2i + 1)的总和。 However, I am not sure how to make my program show the term that must be added in order to reach the number enter by the user. 但是,我不确定如何让我的程序显示必须添加的术语,以达到用户输入的数字。 For example. 例如。 If I enter 12, I want to know how many terms of the series [1/3 + 2/5 + 3/7 + 4/9 + ... + i / (2i + 1)] were added to get approximately to the number 12. 如果我输入12,我想知道系列的多少项[1/3 + 2/5 + 3/7 + 4/9 + ... + i /(2i + 1)]被添加到大约数字12。

What I don't want to get is the sum of inputting 12 which in this case is 5.034490247342584 rather I want to get the term that if I were to sum all numbers up to that term I would get something close to 12. 我不想得到的是输入12的总和,在这种情况下是5.034490247342584而不是我想得到这样的术语,如果我要将所有数字相加到该项,我会得到接近12的东西。

Any help will be greatly appreciated! 任何帮助将不胜感激!

This is my code 这是我的代码

import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {

    double number;
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a value=  ");
    number = input.nextInt();

    System.out.println(sum(number) + " is the  term that should be added in order to reach " + number);

}

public static double sum(double k) {
    if (k == 1) 
        return 1/3;
    else 
        return ((k/(2*k+1))+ sum(k-1));
     }  
}

You have this question kind of inside out. 你有这个问题。 If you want to know how many terms you need to add to get to 12, you'll have to reverse your algorithm. 如果您想知道需要添加多少个术语来获得12,那么您必须反转算法。 Keep adding successive k / (2k + 1) for larger and larger k until you hit your desired target. 继续为更大和更大的k添加连续的k /(2k + 1),直到达到所需的目标。 With your current sum method, you would have to start guessing at starting values of k and perform a sort of "binary search" for an acceptably close solution. 使用当前的sum方法,您必须开始猜测k的起始值并执行一种“二分搜索”以获得可接受的紧密解决方案。

I don't think that this problem should be solved using recursion, but... if you need to implement it on that way, this is a possible solution: 我不认为这个问题应该使用递归来解决,但是......如果你需要以这种方式实现它,这是一个可能的解决方案:

import java.util.Scanner;

public class Recursion {
    public static void main(String[] args) {

        double number;
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a value=  ");
        number = input.nextInt();

        double result = 0;
        double expectedValue = number;

        int k = 0;
        while (result < expectedValue) {
            k++;
            result = sum(k);
        }

        System.out.println(k
                + " is the  term that should be added in order to reach "
                + number + " (" + sum(k) + ")");

    }

    public static double sum(double k) {
        if (k == 1)
            return 1 / 3;
        else
            return ((k / (2 * k + 1)) + sum(k - 1));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM