Code: Selecteer alles
import java.io.*;
public class BerekenPi {
static int n;
public static void main(String[] args) throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
n=Integer.parseInt(br.readLine());
benaderPi(n);
}
public static void benaderPi(int n){
double segment=Math.sqrt(3)/2;
int i=0;
while(i<n){
System.out.print(segment*3*Math.pow(2,i)+", ");
if(4*((i+1)/4)==i+1)System.out.print('\n');
segment=Math.sqrt((1-Math.sqrt(1-segment*segment))/2);
i++;
}
System.out.print(segment*3*Math.pow(2,n));
}
}
Voorbeeld van output n=30:
Code: Selecteer alles
2.598076211353316, 2.9999999999999996, 3.1058285412302498, 3.132628613281237,
3.139350203046872, 3.14103195089053, 3.1414524722853443, 3.141557607911622,
3.141583892148936, 3.1415904632367617, 3.1415921060430483, 3.1415925165881546,
3.1415926186407894, 3.1415926453212157, 3.1415926453212157, 3.1415926453212157,
3.1415926453212157, 3.141593669849427, 3.1415923038117377, 3.1416086962248038,
3.1415868396550413, 3.1416742650217575, 3.1416742650217575, 3.1430727401700396,
3.1598061649411346, 3.181980515339464, 3.3541019662496847, 4.242640687119286,
6.0, 0.0, 0.0