Skip to content

Instantly share code, notes, and snippets.

@yuikns
Last active August 29, 2015 14:11
Show Gist options
  • Save yuikns/d05269acc2cdd0db47e9 to your computer and use it in GitHub Desktop.
Save yuikns/d05269acc2cdd0db47e9 to your computer and use it in GitHub Desktop.
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