Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Nth-root/Scala/nth-root-1.scala
Normal file
9
Task/Nth-root/Scala/nth-root-1.scala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def nroot(n: Int, a: Double): Double = {
|
||||
@tailrec
|
||||
def rec(x0: Double) : Double = {
|
||||
val x1 = ((n - 1) * x0 + a/math.pow(x0, n-1))/n
|
||||
if (x0 <= x1) x0 else rec(x1)
|
||||
}
|
||||
|
||||
rec(a)
|
||||
}
|
||||
2
Task/Nth-root/Scala/nth-root-2.scala
Normal file
2
Task/Nth-root/Scala/nth-root-2.scala
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def fallPrefix(itr: Iterator[Double]): Iterator[Double] = itr.sliding(2).dropWhile(p => p(0) > p(1)).map(_.head)
|
||||
def nrootLazy(n: Int)(a: Double): Double = fallPrefix(Iterator.iterate(a){r => (((n - 1)*r) + (a/math.pow(r, n - 1)))/n}).next
|
||||
Loading…
Add table
Add a link
Reference in a new issue