Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created November 20, 2009 11:15
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 yuroyoro/239431 to your computer and use it in GitHub Desktop.
Save yuroyoro/239431 to your computer and use it in GitHub Desktop.
//ガウス=ルジャンドルのアルゴリズム
// あとでBigDecimalになおす
def calcPI( ac:Int ):Double = {
def stop_?( d:Double ) = d.toString.length > ac + 2
def calc( a:Double , b:Double, t:Double, p:Double ):Double = {
println("%f, %f, %f, %f".format( a, b, t, p ) )
if( stop_?( a ) && stop_?( b ) ) Math.pow( a + b , 2) / (4 * t)
else{
val x = ( a + b ) / 2
val y = Math.sqrt( a * b )
val t2 = t - p * Math.pow( a - x, 2 )
calc( x, y, t2, p * 2 )
}
}
calc( 1, Math.sqrt( 0.5 ), 0.25, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment