Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Nth-root/Kotlin/nth-root.kotlin
Normal file
21
Task/Nth-root/Kotlin/nth-root.kotlin
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun nthRoot(x: Double, n: Int): Double {
|
||||
if (n < 2) throw IllegalArgumentException("n must be more than 1")
|
||||
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
|
||||
val np = n - 1
|
||||
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
|
||||
var g1 = x
|
||||
var g2 = iter(g1)
|
||||
while (g1 != g2) {
|
||||
g1 = iter(g1)
|
||||
g2 = iter(iter(g2))
|
||||
}
|
||||
return g1
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val numbers = arrayOf(1728.0 to 3, 1024.0 to 10, 2.0 to 2)
|
||||
for (number in numbers)
|
||||
println("${number.first} ^ 1/${number.second}\t = ${nthRoot(number.first, number.second)}")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue