Assigned Question:
The infinite series:
" 1 + 22 * x + 32 * x2 + 42 * x 3 + ....."
Each term in the series is made up of a coefficient multiplying a power of x. The coefficients are the squares of the successive integers starting with 1, and the powers uses successive integers, starting with 1, and the powers uses successive exponents starting at 0. The sum converges when |x| is less than 1.
Write a program that reads x, verifies that |x| < 1 or else crashes, and then computes and outputs the sum together with the oracle's answer:
Oracle is: (1 + x) / (1 - x)3
To compute the sum, keep adding terms until you reach a term whose value is less than 0.0001 in absolute value.
I wrote the program.. but something is missing because when I compiled it & ran it... my program would keep printing the correct value ( the answer of the series of the sum) an infinite amount of times.. How do I fix it?
Here is my program:
import java.io.PrintStream; import java.util.Scanner; import type.lib.ToolBox; public class Check07C { public static void main(String[] args) { PrintStream out = System.out; Scanner in = new Scanner(System.in); out.print("Enter the value of x ... "); double x = in.nextDouble(); double series = 0; if (Math.abs(x) > 1.0) { ToolBox.crash(true, "|x| must be < 1."); } else { double y = Math.pow(1 - x, 3); double oracle = (1 + x) / y; out.println("The Oracle's answer is: " + oracle); for (int i = 0; ;i++) { double pow = Math.pow(i, 2); double pow1 = Math.pow(x, i - 1.0); double term = pow * pow1; if (Math.abs(term) < 0.0001) { out.println("The sum of the series = " + series); } else { series = series + term; } } } } }