Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. public static void main(String[] args) throws Exception {
  2. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  3. System.out.println("Enter the number of calculations you would like to do");
  4.  
  5.  
  6. long no = Long.parseLong(reader.readLine());
  7. long cycle = 0;
  8. long w = 2;
  9. long x = 3;
  10. long y = 4;
  11. long z = 4;
  12. long odd=1;
  13. long i=1;
  14. long a = 1;
  15. long b = 1;
  16. double pi= 0.0;
  17.  
  18. for(;i<=no;i++)
  19. {
  20. a = w*x*y;
  21. b = x*y*z;
  22. double currentTerm=0.0;
  23. if (i%2==0)
  24. {
  25. currentTerm=(double)4/a;
  26. cycle = cycle+1;
  27. w = w+1;
  28. x = x+1;
  29. y = y+1;
  30. }
  31. else
  32. {
  33. currentTerm=(double)-4/b;
  34. cycle = cycle+1;
  35. x = x+1;
  36. y = y+1;
  37. z = z+1;
  38. }
  39. odd=odd+2;
  40. pi = pi+currentTerm;
  41.  
  42. }
  43.  
  44. System.out.println("You calculated that pi is");
  45. System.out.println(pi);
  46. System.out.println(3.1415926535897932);
  47. System.out.println("Pi is actually");
  48.  
  49. double error = pi/3.1415926535897932;
  50. if(error >= 1) {
  51. double bigerror=2-error;
  52. System.out.println("Your accuracy is");
  53. System.out.println(bigerror*100);
  54. System.out.println("percent");
  55. System.out.println(cycle);
  56. }
  57. else {
  58. System.out.println("Your accuracy is");
  59. System.out.println(error*100);
  60. System.out.println("percent");
  61. System.out.println(cycle);
  62. }
  63. }
  64. }
  65.  
  66. import java.io.BufferedReader;
  67. import java.io.InputStreamReader;
  68.  
  69. public class MainProgram {
  70.  
  71. public static void main(String[] args) throws Exception {
  72. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  73. System.out.println("Enter the number of calculations you would like to do");
  74.  
  75. long no = Long.parseLong(reader.readLine());
  76. long step = 0;
  77. double ans = 3;
  78. long j = 2;
  79.  
  80. double pi = 0.0;
  81.  
  82. while (true) {
  83. step++;
  84. if ((step % 2) == 1) {
  85. ans += 4.0 / (1.0 * j * (j + 1) * (j + 2));
  86. } else {
  87. ans -= 4.0 / (1.0 * j * (j + 1) * (j + 2));
  88. }
  89.  
  90. j += 2;
  91. pi = ans;
  92.  
  93. if (step >= no)
  94. break;
  95. }
  96.  
  97. System.out.println("You calculated that pi is");
  98. System.out.println(pi);
  99. System.out.println("Pi is actually");
  100. System.out.println(3.1415926535897932);
  101.  
  102. double error = pi / 3.1415926535897932;
  103. if (error >= 1) {
  104. double bigerror = 2 - error;
  105. System.out.print("Your accuracy is: ");
  106. System.out.print(bigerror * 100);
  107. System.out.println(" percent");
  108. System.out.println(step);
  109. } else {
  110. System.out.print("Your accuracy is: ");
  111. System.out.print(error * 100);
  112. System.out.println(" percent.");
  113. System.out.println(step);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement