Friday, March 13, 2015

In honor of pie day, a function that will approximate pi more and more the larger positive number you give it. Note that it will exceed your stack before it gets very far. wink emoticon This is my stalling-before-bedtime attempt to implement 4/1 - 4/3 + 4/5 - 4/7 + etc.

var pie = function(x) { 
    x = (x%2==0) ? x+1 : x ;
    if (x < 2) { 
        return 4; 
    } else { 
        return pie(x-2) + (1- (2*(((x-1)/2)%2))) * (4/x); 
    }
}
In my current instance of Chrome, in the dev console, I can enter up to pie(35859), which returns 3.1415368811414246.

No comments:

Post a Comment