Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter the number of calculations you would like to do");
- long no = Long.parseLong(reader.readLine());
- long cycle = 0;
- long w = 2;
- long x = 3;
- long y = 4;
- long z = 4;
- long odd=1;
- long i=1;
- long a = 1;
- long b = 1;
- double pi= 0.0;
- for(;i<=no;i++)
- {
- a = w*x*y;
- b = x*y*z;
- double currentTerm=0.0;
- if (i%2==0)
- {
- currentTerm=(double)4/a;
- cycle = cycle+1;
- w = w+1;
- x = x+1;
- y = y+1;
- }
- else
- {
- currentTerm=(double)-4/b;
- cycle = cycle+1;
- x = x+1;
- y = y+1;
- z = z+1;
- }
- odd=odd+2;
- pi = pi+currentTerm;
- }
- System.out.println("You calculated that pi is");
- System.out.println(pi);
- System.out.println(3.1415926535897932);
- System.out.println("Pi is actually");
- double error = pi/3.1415926535897932;
- if(error >= 1) {
- double bigerror=2-error;
- System.out.println("Your accuracy is");
- System.out.println(bigerror*100);
- System.out.println("percent");
- System.out.println(cycle);
- }
- else {
- System.out.println("Your accuracy is");
- System.out.println(error*100);
- System.out.println("percent");
- System.out.println(cycle);
- }
- }
- }
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class MainProgram {
- public static void main(String[] args) throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter the number of calculations you would like to do");
- long no = Long.parseLong(reader.readLine());
- long step = 0;
- double ans = 3;
- long j = 2;
- double pi = 0.0;
- while (true) {
- step++;
- if ((step % 2) == 1) {
- ans += 4.0 / (1.0 * j * (j + 1) * (j + 2));
- } else {
- ans -= 4.0 / (1.0 * j * (j + 1) * (j + 2));
- }
- j += 2;
- pi = ans;
- if (step >= no)
- break;
- }
- System.out.println("You calculated that pi is");
- System.out.println(pi);
- System.out.println("Pi is actually");
- System.out.println(3.1415926535897932);
- double error = pi / 3.1415926535897932;
- if (error >= 1) {
- double bigerror = 2 - error;
- System.out.print("Your accuracy is: ");
- System.out.print(bigerror * 100);
- System.out.println(" percent");
- System.out.println(step);
- } else {
- System.out.print("Your accuracy is: ");
- System.out.print(error * 100);
- System.out.println(" percent.");
- System.out.println(step);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement