September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View 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)
}

View 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

View file

@ -1,16 +0,0 @@
object NthRoot {
def main(args: Array[String]) {
println(nthroot(3, 32))
}
def nthroot1(n: Int, a: Double): Double = {
def loop(x0: Double) : Double = {
val x1 = (1.0d/n * ((n - 1) * x0 + a/math.pow(x0, n-1)))
if (x0 <= x1) x0
else loop(x1)
}
return loop(a/2)
}
}