[removed] PI (π) Calculator

前端 未结 5 1824
忘了有多久
忘了有多久 2021-01-26 04:43

Is there a way to calculate pi in Javascript? I know there you can use Math.PI to find pie like this:

var pie = Math.PI;
alert(pie); // output \"3.1         


        
5条回答
  •  时光取名叫无心
    2021-01-26 05:12

    Here is my implementation using Infinite Series

    function calculatePI(iterations = 10000){
        let pi = 0;
        let iterator = sequence();
    
        for(let i = 0; i < iterations; i++){
            pi += 4 /  iterator.next().value;
            pi -= 4 / iterator.next().value;
        }
    
        function* sequence() {
          let i = 1;
          while(true){
            yield i;
            i += 2;
          }
        }
    
        return pi;
    }
    

提交回复
热议问题