My daughter was given the following task: given the first number x and the second number y, find the nth number. The description was quite vague, but my daughter managed to do the task.
Can you?
Example
Turns out, for x = 7, y = 10 and n = 5,
the output should be
nthPlace(x, y, n) = 19.
Input/Output
- [time limit] 4000ms (js)
[input] integer x
The first number.
Constraints:
1 ≤ x ≤ y.[input] integer y
The second number.
Constraints:
x ≤ y ≤ 100.[input] integer n
The number to find.
Constraints:
3 ≤ n ≤ 50.[output] integer
The nth number.
Solution
The solution is simply \(n-1\) times the difference of the upper and lower bound plus the lower bound:
\[s = x + (y - x) \cdot (n - 1)\]
Or as a JavaScript solution:
nthPlace = (x, y, n) =>
x + (y - x) * --n