Please code in JAVA . Compute PI by Kashi\'s Method (use iteration, not recursio
ID: 3875752 • Letter: P
Question
Please code in JAVA.
Compute PI by Kashi's Method (use iteration, not recursion). Let a regular polygon be inscribed in a unit circle such that each side, an, subtends an arc = 607 2-1 , and let cn be the chord of the supplement of this arc. Then, since = 3, the recursion relation = (2 + Cr-1 )L2 en makes it possible to calculate any desired cn. Moreover, the Pythagorean relation gives a, = (22-c, ) and half of the perimeter, an approximation to , is the product of the length of a side, times the number of sides, ½·3·2"Explanation / Answer
/**
* Write a description of class KashiPi here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
import java.util.*;
public class KashiPi
{
// instance variables - replace the example below with your own
private int x;
private int cn,n;
// Main method to ask for user input : the number of iterations
public static void main (String[] args)
{
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int.
//once finished
reader.close();
CalculatePi(n);
}
/**
* Constructor for objects of class KashiPi
*/
public KashiPi()
{
// initialise instance variables
cn = 1;
n=1;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public static void CalculatePi(int iterationcount)
{
// put your code here
double pi = 1;
int twopowern = 1;
double cnseq = 1;
for (int i = 1; i <= iterationcount;i++)
{
// This is how we can calculate pi using Kashi Algorithm
// pi = 3 * 2^n * Cn
// where n is number of iterations or precision we expect
// and Cn = SQRT(2-SQRT(4-Cn-1^2))
twopowern = twopowern * 2;
cnseq = Math.sqrt(2-Math.sqrt(4-(cnseq*cnseq)));
}
pi = 3 * twopowern * cnseq;
System.out.println("Pi is :"+ pi);
}
}