Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem:
- // The value of pi can be determined by the series equation:
- // pi = 4(1-1/3+1/5-1/7+1/9-1/11+...)
- // Write a program to approximate the value of pi using the formula given including terms up through 1/99.
- public class taylorpi{
- static double estimate=0.0;
- static double max_int=99.0;
- static String factor_sign = "+";
- public static void setFactorials(){
- double i = 1.0;
- while(i <= taylorpi.max_int){
- if(taylorpi.factor_sign == "+"){
- taylorpi.estimate += (1.0/i);
- i+=2;
- taylorpi.factor_sign = "-";
- }else if(taylorpi.factor_sign == "-"){
- taylorpi.estimate -= (1.0/i);
- i+=2;
- taylorpi.factor_sign = "+";
- }
- }
- }
- public static void main(String[] args){
- taylorpi.setFactorials();
- taylorpi.estimate *= 4;
- System.out.println(taylorpi.estimate);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement