Mathematically, computing the sum S = Sigma^n_i=1 1/i(I + 1) as A = 1/1(1 + 1) +
ID: 3663587 • Letter: M
Question
Mathematically, computing the sum S = Sigma^n_i=1 1/i(I + 1) as A = 1/1(1 + 1) + 1/2(2 + 1) + ...+ 1/n(n + 1) should be equal to computing it as B = 1/n(n + 1) +...+ 1/2(2 + 1) + 1/1(1 + 1). Additionally, note that the closed form of this sum is S = n/n + 1. Write a computer program to take an input variable n, evaluate A and B (with the sum computed in the order from left to right as shown above), and compute the absolute errors S - A and S - B using the closed form of S. Run your program 6 times to provide input values of n of 10, 50, 100, 500, 1000, and 5000, and tabulate and analyze your results. Please include your source code in addition to your tabulated and analyzed results in your homework.
Explanation / Answer
Answer:
import java.util.*;
class Ordercode
{
public static void main(String args[])
{
int i;
int z=0;
int temp=0;
int y=0;
Scanner in = new Scanner(System.in);
double k=0.0;
double calc=0;
double s=0.0;
//int n=10;
System.out.println("Enter value of n:");
int n = in.nextInt();
System.out.println("You entered integer "+n);
double m=0.0;
for(i=1;i<=n;i++)
{
z=i*(i+1);
//System.out.println(z);
k=1/(double)z;
//System.out.println(k);
y=z*4;
//System.out.println(y);
calc=10/(double)y;
m=n/(double)(n+1);
}
System.out.println("calc is "+calc);
System.out.println("m is "+m);
double finalcalc=m-calc;
System.out.println("final calc is"+finalcalc);
//System.out.println(k);
//System.out.println(n);
}
}