fork download
  1. object Main extends App {
  2. import java.math.{BigDecimal => JDecimal}
  3. import java.math.RoundingMode._
  4. import scala.concurrent.Future
  5. import scala.concurrent.Await
  6. import scala.concurrent.ExecutionContext.Implicits._
  7. import scala.concurrent.duration._
  8. val precision = 1800
  9.  
  10. def acotPrecision(numDigits: Int)(x: BigDecimal) = {
  11. val x1 = x.underlying
  12. val two = JDecimal.valueOf(2)
  13. val xSquared = x1 pow 2
  14. val unity = JDecimal.ONE.setScale(numDigits, HALF_EVEN)
  15. var sum = unity.divide(x1, HALF_EVEN)
  16. var xpower = new JDecimal(sum.toString)
  17. var term = unity
  18.  
  19. var add = false
  20.  
  21. var n = JDecimal.valueOf(3).setScale(numDigits)
  22. while (term.setScale(numDigits, HALF_EVEN).compareTo(JDecimal.ZERO) != 0) {
  23. xpower = xpower.divide(xSquared, HALF_EVEN)
  24. term = xpower.divide(n, HALF_EVEN)
  25. sum = if (add) sum add term else sum subtract term
  26. add = !add
  27. n = n add two
  28. }
  29. sum
  30. }
  31.  
  32. def ePrecision(numDigits: Int) = {
  33. val zero = JDecimal.ZERO
  34. var sum = zero
  35. var term = JDecimal.ONE.setScale(numDigits, HALF_EVEN)
  36. var n = JDecimal.ONE.setScale(numDigits, HALF_EVEN)
  37. while(term.setScale(numDigits, HALF_EVEN).compareTo(zero) != 0) {
  38. sum = sum add term
  39. term = term.divide(n, HALF_EVEN)
  40. n = n add JDecimal.ONE
  41. }
  42. sum
  43. }
  44.  
  45. val acot = acotPrecision(precision) _
  46.  
  47. def d(x: Int) = JDecimal.valueOf(x)
  48.  
  49. def piFuture = Future(d(4) multiply (
  50. (d(83) multiply acot(107)) add (d(17) multiply acot(1710)) subtract (d(22) multiply acot(103697))
  51. subtract (d(24) multiply acot(2513489)) subtract (d(44) multiply acot(18280007883L))
  52. add (d(12) multiply acot(7939642926390344818L))
  53. add (d(22) multiply acot(BigDecimal("3054211727257704725384731479018")))
  54. ))
  55.  
  56. def eFuture = Future(ePrecision(precision))
  57.  
  58. Await.result(
  59. for (pi <- piFuture;
  60. e <- eFuture) yield println((pi multiply e).setScale(precision - 10, DOWN))
  61. , 5 seconds)
  62. }
Success #stdin #stdout 4.71s 383360KB
stdin
Standard input is empty
stdout
8.53973422267356706546355086954657449503488853576511496187960113017922861115733080757256386971047394391377494251167746764632118759069602399061836345379070414542021599488963342852746700046687766093072711290393507480401055727040348627303998656540644166179229285713708216374412976168471172544672318420340751657873020506707999472076298967964373713900900839870785220633048298035384640173153001978236276770258035741255972055172639898617344959092612412289680764582785420543163215795419510261753326139327091269239274357568312735470622184259342983913191813594440620442770198489377232444615908940370060151390186645874739849823655400651269457195509817518967609862391220148508155545607981568675308586739080896645836366491171197692951525938412546338435876040814802615808583287673599471606966708191549941538446985380682339134628186666645715222960481458039095498952067967593111092538597541522316434620440904327905669010198992873083145479079327160729091948477188491992878937162484609450111652203196731971058211832709336231538251949398510926142750438850908482455431257277861356265114867397731658232289198374727773111695158465972587810000329741873624229923620712202587599094699590040464624319591158102542086129537177924294339770576617165650922379113217960851065792956862982185232418919771948419951864698015292439230706583349211674611155609277272183291558654704961469794355906270234465129223132883716678810091064920511620557569628569622711445864545823007560422382107955234019816469501880652419579034206487408747628701106957435990553220320125711611422213721382074566116348452622815821743290221020783462683275622959445086528114489829543104972972636954839998437938010998380887782568954452643089798722258988037308148878697934681308369634030997216325616455981696424010586563370264659416464674890754789241939704197023