Estimating #1 The value of can be estimated by the infinite series =4(1-1+1-1+1-
ID: 3879927 • Letter: E
Question
Estimating #1 The value of can be estimated by the infinite series =4(1-1+1-1+1-...) The greater the number of terms used in the calculation, the more accurate the estimate. write a function that takes a single integer N and estimates usng the first N terms of this series. Part A: Write a program that prompts the user for N then outputs the estimated value of pi. Part B: Assuming that math.pi is the correct value for pi, prompt the user for the desired accuracy, then find the minimum value of N that delivers that accuracy. ser wants the estimate to be within +/-0.0001, determine the minimum number of term:s required to achieve that level of accuracy. For example,if the uExplanation / Answer
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;
public class PiEstimation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DecimalFormat format = new DecimalFormat("##.#####");
format.setRoundingMode(RoundingMode.UP);
System.out.println("1. Estimated Value for Nth Terms 2. Estimate Accuracy");
int choice = scanner.nextInt();
PiEstimation obj = new PiEstimation();
switch (choice) {
case 1:
System.out.println("Enter the Nth Term for PI Calculation : ");
int nthTerm = scanner.nextInt();
double estimatedValue = obj.calculatePi(nthTerm);
System.out.println("Estimated PI Value : " + format.format(estimatedValue));
break;
case 2:
System.out.println("Enter Desired Accuracy Level : ");
int accuracy = scanner.nextInt();
String precision = "#.#";
for (int i = 1; i < accuracy; i++) {
precision += "#";
}
System.out.println("Accuracy Found at : " + obj.getN(precision) + "th Term.");
break;
default:
System.out.println("Not a valid choice");
}
}
/**
* Calculate the Nth term value for the given formula
*
* @param n
* @return
*/
public double calculatePi(int n) {
boolean signFlipper = false;
double val = 1.0;
double estimateValue = 0;
for (int i = 1; i <= n; i++) {
double term1 = 1 / val;
if (i > 1) {
signFlipper = !signFlipper;
if (signFlipper) {
estimateValue -= term1;
} else {
estimateValue += term1;
}
} else
estimateValue = term1;
val += 2;
}
return (4.0 * estimateValue);
}
/**
* Estimate the Nth term which matches with Math.PI within user specified precision
*
* @param accuracyPrecisionFormat
* @return
*/
public int getN(String accuracyPrecisionFormat) {
System.out.println("pattern : "+accuracyPrecisionFormat);
DecimalFormat format = new DecimalFormat(accuracyPrecisionFormat);
format.setRoundingMode(RoundingMode.UP);
double piValue = Math.PI;
double estimatedValue;
int i = 1;
boolean signFlipper = false;
double val = 1.0;
double estimateValue = 0;
while (true){
double term1 = 1 / val;
if (i > 1) {
signFlipper = !signFlipper;
if (signFlipper) {
estimateValue -= term1;
} else {
estimateValue += term1;
}
} else
estimateValue = term1;
val += 2;
double tempEstimatedValue=(4.0*estimateValue);
if (format.format(tempEstimatedValue).equals(format.format(piValue))){
estimatedValue=tempEstimatedValue;
break;
}
i++;
}
System.out.println("Original Value : "+format.format(piValue));
System.out.println("estimated Value : "+format.format(estimatedValue));
return i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.