Last active
August 29, 2015 14:11
-
-
Save yuikns/d05269acc2cdd0db47e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pi1(lsz: Int): Double = { | |
var a = 3.0 | |
var off = 1 | |
var lcnt = lsz | |
def p_elem(t: Int): Double = 4.0 / ((2 * t) * (2 * t + 1) * (2 * t + 2)) | |
while (lcnt > 0) { | |
if (off % 2 == 0) { | |
a -= p_elem(off) | |
} else { | |
a += p_elem(off) | |
} | |
off += 1 | |
lcnt -= 1 | |
} | |
a | |
} | |
def pi2(lsz: Int): Double = { | |
var a = 3.0 | |
def p_elem(t: Int): Double = 4.0 / ((2 * t) * (2 * t + 1) * (2 * t + 2)) | |
(1 to lsz).toArray.par.foreach(b => { | |
if (b == 0) { | |
a | |
} else if (b % 2 == 0) { | |
a -= p_elem(b) | |
} else { | |
a += p_elem(b) | |
} | |
}) | |
a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment