CALCULO DEL NÚMERO PI POR EL ALGORITMO DE:

 

Bailey-Borwein-Plouffe

 

 

 

 

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import java.math.*;

import java.util.*;

/**

 * This class reads PARAM tags from its HTML host page and sets

 * the color and label properties of the applet. Program execution

 * begins with the init() method.

 */

public class calculopi extends Applet implements ActionListener,Runnable

{

               

                Button calcular;

                Label pi;

                Label texto;

                Label numerodigitos;

 

                Panel panel1,panel2,panel3;

                TextField digitos;

                double dig;

                public Thread t = null;

                double contador, maximocontador , Pi ,valor,max;

                boolean menosuno;

               

                public void calculopi()

                {

               

                }

               

                public void init()

                {

                               super.init();

                               FlowLayout rejilla = new FlowLayout();

                               setLayout(new BorderLayout());

                               panel1= new Panel();

                               panel2=new Panel();

                               panel3=new Panel();

                               calcular=new Button("Pulsar para Calcular");

                               pi = new Label("Número PI                                  ");

                               numerodigitos = new Label("Numero de Ciclos del bucle de cálculo");

                               digitos = new TextField(10);

                               digitos.setText("100");

                               texto = new Label("Pulse para calcular...");             

                               panel1.add(calcular);

                               panel1.add(pi);

                               panel2.add(texto);

                               panel2.add(new Label("Se Utiliza el algoritmo de Bailey-Borwein-Plouffe"));

                               panel3.add(numerodigitos);

                               panel3.add(digitos);

                               add("North",panel1);

                               add("Center",panel2);

                               add("South",panel3);

                               calcular.addActionListener(this);

                              

                               }

                               public void actionPerformed(ActionEvent e)

                               {

                                               if(t!=null)

                                               {

                                                               t=null;

                                               }

                                               if (t==null)

                                               {

                                                               menosuno=true;

                                                               Pi=0;

                                                               valor=0;

                                                               maximocontador=10;

                                                               dig=1;

                                                               pi.setText("Comienzo a calcular");                                                                            

                                                               t = new Thread(this);

                                                               t.start();

                                                               texto.setText("Terminado el calculo");

                                               }

               

                                              

                               }

               

                              

                               public void run()

                               {

               

                                               while(t!=null)

                                               {

                                                               max= Double.valueOf(digitos.getText()).doubleValue();

                                                               for(double i=0; i<max;i++)

                                                               {             

                                                                              Pi = Pi + 1/Math.pow(16,i)* (4 /( 8 * i + 1 ) - 2 / ( 8 * i + 4 ) - 1 / ( 8 * i + 5 )- 1 /(8 * i + 6));

                                                                              pi.setText(Double.toString(Pi));

                                                               }

                                                               t.stop();

                                                               t=null;

                                               }

                               }

}