Hi,

I programmed this code to solve the equation:

x/2! + x^n-1/n! where n is the size of series

The program functions, but appears to be giving me the incorrect result.

For example if I enter 3 for n, and 2 for x I am expecting 1.67 as the result but am getting 1.36

Could you pleae take a look at my for loops where the solution is being calculated and see if my logic there is wrong, and if it is, could you please tell me what to fix.

I've tried removing the inner loop and making the outer loop instead:

for (int i = 2; i <= nth; i++) {
  solution += ((Math.pow(x, i-1)) / factorial(2));
}

but that when n = 3, and x = 2 gives a result of 2 which is also wrong

import java.util.Scanner;
import java.text.DecimalFormat;
public class SolveEquation {
  static int factorial = 1;
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    DecimalFormat df = new DecimalFormat("0.##");
    double solution = 0;

    System.out.println("Solve the series x/2! + x^2/3! + x^3/4! ... x^nth-1/nth!");
    System.out.println("Enter the nth value");
    int nth = input.nextInt();

    System.out.println("Enter a value for x");
    double x = input.nextDouble();    

    for (int i = 1; i < nth; i++) {
      for (int j = 2; j <= nth; j++) {
        solution += ((Math.pow(x, i)) / factorial(j));      
      }
    }      
    System.out.println(df.format(solution));    
  }
  public static int factorial(int n) {
    for (int i = 1; i <= n; i++) {
      factorial *= i;
    }
    return factorial;
  }
}

Recommended Answers

All 5 Replies

Your factorial method doesn't initialise the variable "factorial", so each time you call it factorial will keep getting bigger and bigger. That variable should be a local var in the method, initialised at the start of the method.

OK, so I added in my factorial method before the for (condition) loop:

factorial = n;

Now when I enter 3 for n, and 2 for x I get a result of 1.83; but unless my math is wrong is not:

2/2! + 2^2/3! = 1 + 4/6 = 1.67 (2dp)

That's not a sensible value to initialise your factorial. Eg, if n=3 your factorial will be
312*3

Why not take a minute or two to debug that method fully (send it some simple values for n and print what it returns) before you try to debug other code that depends on it.

Thank you James,

I wrote a separate class and tested the factorial method as you suggested.

It soon became obvious that integer before loop needed to be equal to 1.

Then afer commenting out my other for loops and printing the values of i and j, I saw my other problem.

So no in the main method I am using only the one loop:

for (int i = 2; i <= nth; i++) {
  solution += ((Math.pow(x, i-1)) / factorial(i)); 
} 

and in my factorial method before the for (condition) I put:

int factorial = 1;

The code finally works as expected.

Thank you very much for your sharp logic. Mine was clearly hiding in the clouds. :)

The most important message wasn't how to code a series formula - it was how to test and debug code in a methodical step-by-step process. It's amazing how many people present problems here where they have written sometimes many hundreds of lines of code before trying to run them and are totally baffled when the answer is wrong. Experienced programmers test early and test often.

Anyway, glad to help, and thanks for the feedback
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.