Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. // Problem:
  2. // The value of pi can be determined by the series equation:
  3.  
  4. // pi = 4(1-1/3+1/5-1/7+1/9-1/11+...)
  5. // Write a program to approximate the value of pi using the formula given including terms up through 1/99.
  6.  
  7.  
  8. public class taylorpi{
  9. static double estimate=0.0;
  10. static double max_int=99.0;
  11. static String factor_sign = "+";
  12. public static void setFactorials(){
  13. double i = 1.0;
  14. while(i <= taylorpi.max_int){
  15. if(taylorpi.factor_sign == "+"){
  16. taylorpi.estimate += (1.0/i);
  17. i+=2;
  18. taylorpi.factor_sign = "-";
  19.  
  20. }else if(taylorpi.factor_sign == "-"){
  21. taylorpi.estimate -= (1.0/i);
  22. i+=2;
  23. taylorpi.factor_sign = "+";
  24. }
  25. }
  26. }
  27. public static void main(String[] args){
  28. taylorpi.setFactorials();
  29. taylorpi.estimate *= 4;
  30. System.out.println(taylorpi.estimate);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement