How do i write a recursive method to display x^0 + x^1 + x^2 + … x^n and then display the computed values of x^n?

☆樱花仙子☆ 提交于 2021-01-07 03:22:27

问题


I want to write a recursive method to compute

x^0 + x^1 + x^2 + ... x^n

and then i want to display the values for the computed x^n ex:

n = 3 and x =3 the output should be : 1 + 3 + 9 + 27

I have code to compute a recursive method like this but how do i use that method to print each iteration?

My code is :

public static double compute(int n, int x){
   
    if(n == 0){
        return 1;
    }
    else{
        
        return Math.pow(x , n) + compute(x , n-1);
        
    }
    
}

回答1:


Try this as an alternative. The key is to save the return from Math.pow on the call stack before you print it. That way the terms will be in the correct order when the method unwinds.

double v = compute(3,3);
System.out.println("\nv = " + v);
    
    
public static double compute(int n, int x) {
    double v;
    if (n == 0) {
       System.out.print(1.);
       return 1;
   }
   // save a copy of the power method for printout
   double r = compute(n-1,x)+ (v = Math.pow(x,n));
   System.out.print(" + " + v);
   return r;
}

Prints

1.0 + 3.0 + 9.0 + 27.0
v = 40.0

If you don't want floating point values in your power series display, then just cast v to an int




回答2:


You simply need to call System.out.print() on each recursive call:

public static double compute(int n, int x) {
  if (n == 0) {
    System.out.print("1");  // print 1 (no "+" here because it's the last call)
    return 1;
  } else {
    double currVal = Math.pow(x, n);   // value for the current x and n
    System.out.print(String.valueOf(currVal) + " + ");  // print currVal with "+"
    return currVal + compute(n - 1, x);  // make recursive call
  }
}



回答3:


I mean, you print the number when you reach there:

public static double compute(int n, int x){
   
    if(n == 0){
        System.out.print(1);//It's 1...
        return 1;
    }
    else{
        int number = (int)Math.pow(x , n);
        System.out.print(number + "+");//since n is not 0, there must be data printed after this 
        return number + compute(n - 1 , x);
    }
    
}

Or if you want to print from smallest to the largest:

    public static double compute(int n, int x){    
        if(n == 0){
            System.out.print(1);//It's 1...
            return 1;
        }
        else{
            int number = (int)Math.pow(x , n);
            try {//needed for the finally statement
                return number + compute(n - 1 , x);
            }finally {//finaly block will be executed even after return
                System.out.print("+" + number);//since n is not 0, there must be data printed before this
            } 
        }
        
    }



回答4:


public class Demo {

public static StringBuilder stringBuilder = new StringBuilder();

public static double compute(int n, int x) {

    if (n == 0) {
        stringBuilder.insert(0, x + "^" + n);
        return 1;
    } else {
        stringBuilder.insert(0, " + " + x + "^" + n);
        return Math.pow(x, n) + compute(n - 1, x);
    }
}

public static void main(String[] args) {
    System.out.println(stringBuilder.append(" = ").append(compute(5, 2)));

}

}



来源:https://stackoverflow.com/questions/64885550/how-do-i-write-a-recursive-method-to-display-x0-x1-x2-xn-and-then

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!