How to Find the Value of Pi to a Great Level of Accuracy.

by ruhanhabib39 in Circuits > Tools

2365 Views, 6 Favorites, 0 Comments

How to Find the Value of Pi to a Great Level of Accuracy.

my_code.png
I have found a series that can calculate PI to a great deal of accuracy with less terms.
I'll do that using JavaScript.

See the Formula We Use

pi.png
The day was boring and I so to cut of the boredom, I searched for some formula to calculate PI. Then I found this formula and used it in the JavaScript. Just take a look at the formula:

Translating the Formula to JavaScript

my_code.png
Then I used a while loop ( for-loop would have been better, but I forgot that) to use the formula up to accuracy you want. I have the formula:
                                       3 + 4 / ( 2 * 3 * 4) + 4 / (4 * 5 * 6) + .....
The numbers at the denominator increases by 2 and we have used x+1 is 3 and x+2 is 4. SO we get 3 + 4/(2*(2+1)*(2+2))+.....
We use x instead of  2 because we need to add 2 to all of them every time. And now look.
Just use it if you want.

<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="latextit.js"></script>
  <script type="text/javascript">
   $(document).ready(function(){
       $('#button').click(function(){
           var value = $('input[name=accuracyPercent]').val();

           if(parseInt(value) < 2 ){
            alert("It should be more than " + parseInt(value) + " ");
           }else if(parseInt(value) > 80 ){
            alert("It should be less than " + parseInt(value) + " ");
           }else{
            //first lower
      var x = 2;
      // the variable to collect the estimation of pi
      var n = 3;
      //initial term
      var i = 2;
      var accuracy = parseInt(value) * 2000;//while loop
      while(i <= accuracy){
          //if the  i is even, then do this
          if(i % 2 === 0){
              // the formula
              n += (4 / ( x * ( x + 1) * ( x + 2)));
          }else{ // else you do this
              // the formula
              n -= (4 / ( x * ( x + 1) * ( x + 2)));
          }
          // add 2 to x
          x += 2;
          // i
          i++;
      }
      $('#item').replaceWith('<div id = "item">' + n + '</div>');
     }
       });
    $('#button').mouseover(function(){
     $(this).css('color', '#fff');
    });
    $('#button').mouseleave(function(){
     $(this).css('color', '#000')
    });
   });
  </script>
  <style type="text/css">
   body{
    background: -webkit-linear-gradient(-45deg, #0ff, blue); /* For Safari */
    background: -o-linear-gradient(-45deg, #0ff, blue); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(-45deg, #0ff, blue); /* For Firefox 3.6 to 15 */
    background: linear-gradient(-45deg, #0ff, blue); /* Standard syntax */
   }
   form{
    display: inline-block;
    background-color: blue;
   }
   #button{
       display: inline-block;
       height:20px;
    width:70px;
    background-color:#cc0000;
    font-family:arial;
    font-weight:bold;
    color:#000;
    border-radius: 5px;
    text-align:center;
    margin-top:2px;
   }
   #item{
    text-align: center;
    font-size: 40px;
    color: #000;
   }
   .list{
    font-size: 20px;
   } 
  </style>
</head>
<body>
  <form name="accuracyPercent">
   <input type="text" name="accuracyPercent"/>
  </form>
  <div id="button">Get!</div>
  <p class = "list">
   Let us find the value of <span lang="latex">$\pi$</span> to your accuracy point. (Remember, your value should be greater than 2, less than 1000 and an integer):
  </p>
  <div id = "item">
   3.14159 26535 89793 23846
  </div>
</body>
</html>