Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created June 14, 2015 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/c7a343ed7e2f54acc57a to your computer and use it in GitHub Desktop.
Save antimatter15/c7a343ed7e2f54acc57a to your computer and use it in GitHub Desktop.
Hello Carbide
// You can directly `require` any package from NPM,
// and it'll automatically load the latest version
// and recursively satisfy all dependencies
var bigInt = require("big-integer");
// Code is automatically transpiled with Babel.JS
// so you can use all the ES6/7 features such as
// generators, async functions, fat arrow syntax,
// React JSX, destructuring and more
function* calculatePi(){
let [q, r, t] = [1, 0, 1].map(x => bigInt(x)); // big
let [k, n, l] = [1, 3, 3]
while(true){
if(q.times(4).plus(r).minus(t).lesser(t.times(n))){
yield n
let nr = r.minus(t.times(n)).times(10)
n = +q.times(3).plus(r).times(10).divide(t).minus(n*10)
q = q.times(10)
r = nr
}else{
n = +q.times(k*7).plus(2).plus(r.times(l)).divide(t.times(l))
r = q.times(2).plus(r).times(l)
q = q.times(k)
t = t.times(l)
l += 2
k += 1
}
}
}
// It also automatically injects timing instrumentation
// in outer loops to populate a progress bar with an
// estimate of the current computation progress
var it = calculatePi(), str = '';
for(var i = 0; i < 2000; i++){
str += it.next().value;
}
str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment