Skip to content

Commit 1bbe49c

Browse files
committed
Adiciona CalculatePi.scala
1 parent 829a81d commit 1bbe49c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,8 +3598,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
35983598
</a>
35993599
</td>
36003600
<td> <!-- Scala -->
3601-
<a href="./CONTRIBUTING.md">
3602-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
3601+
<a href="./src/scala/CalculatePi.scala">
3602+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/scala/scala-original.svg" />
36033603
</a>
36043604
</td>
36053605
<td> <!-- Kotlin -->

src/scala/CalculatePi.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def calculatePi(n: Int): Double = {
3+
(0 until n).foldLeft(0.0)((pi, index) => {
4+
val operation: Double = if (index % 2 == 0) 1.0 else -1.0
5+
val denominator: Double = 1.0 + (index * 2.0)
6+
pi + (operation * (4.0 / denominator))
7+
})
8+
}
9+
10+
object Main extends App {
11+
val nTerms: Seq[Int] = Seq(10, 1000, 100000, 10000000)
12+
nTerms.foreach(n => println(s"Pi ($n): ${calculatePi(n)}"))
13+
}

0 commit comments

Comments
 (0)