# Which x for that sum in Kotlin ## The challenge

Consider the sequence `U(n, x) = x + 2x**2 + 3x**3 + .. + nx**n` where x is a real number and n a positive integer.

When `n` goes to infinity and `x` has a correct value (ie `x` is in its domain of convergence `D`), `U(n, x)` goes to a finite limit `m` depending on `x`.

Usually given `x` we try to find `m`. Here we will try to find `x` (x real, 0 < x < 1) when `m` is given (m real, m > 0).

Let us call `solve` the function `solve(m)` which returns `x` such as U(n, x) goes to `m` when `n` goes to infinity.

#### Examples:

`solve(2.0) returns 0.5` since `U(n, 0.5)` goes to `2` when `n` goes to infinity.

`solve(8.0) returns 0.7034648345913732` since `U(n, 0.7034648345913732)` goes to `8` when `n` goes to infinity.

#### Note:

You pass the tests if `abs(actual - expected) <= 1e-12`

## The solution in Kotlin code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solv

fun solve(m:Double):Double {
val s = Math.sqrt(4 * m + 1)
return (2 * m + 1 - s) / (2 * m)
}
```Code language: Kotlin (kotlin)```

Option 2:

``````package solv

import kotlin.math.sqrt

fun solve(m: Double): Double = 1 + (0.5 - sqrt(0.25 + m)) / m
```Code language: Kotlin (kotlin)```

Option 3:

``````package solv

fun solve(s:Double):Double {
return (1 - Math.sqrt(4 * s + 1)) / (2.0 * s) + 1;
}
```Code language: Kotlin (kotlin)```

## Test cases to validate our solution

``````package solv

import org.junit.Assert.*
import org.junit.Test
import java.util.Random

class solvTest {
private fun assertFuzzy(m:Double, expect:Double) {
val merr = 1e-12
println("Testing " + m)
val actual = solve(m)
println("Actual: " + actual)
println("Expect: " + expect)
val inrange = Math.abs(actual - expect) <= merr
if (inrange == false)
{
println("Expected must be near " + expect + ", got " + actual)
}
println("-")
assertEquals(true, inrange)
}
@Test
fun test1() {
assertFuzzy(2.00, 5.000000000000e-01)
assertFuzzy(4.00, 6.096117967978e-01)
assertFuzzy(5.00, 6.417424305044e-01)

}
}
```Code language: Kotlin (kotlin)```
Tags:
Subscribe
Notify of 