Trying to figure out a Mathematical pattern

50 Views Asked by At

I am given a start point, a, an end point b, and a number of values x. With that I am supposed to come up with the points between the start and end point. Below is an example

Given a = 1/2, b = 17/3, and x = 5

1/2
43/24
37/12
38/8
17/3

That list is the answer to the question. I tried figuring this out and looking for a algorithm to do this and found each number differs by 31/24 which I can call y.

My question is, how do I get those middle numbers or better yet, find my y?

1

There are 1 best solutions below

0
On BEST ANSWER

Such a pattern is called an arithmetic progression (Wikipedia).

If you're looking for an arithmetic progression $p_1,p_2,\ldots,p_x$ of length $x$ starting from $p_1=a$ and ending at $p_x=b$, the common difference $y$ you're looking for is $$y=\frac{b-a}{x-1}$$ That way, you get $$\begin{array}{c|c|c|c|c|} i & 1 & 2 & 3 & \cdots & x\\\hline p_i & a & a+1\left(\frac{b-a}{x-1}\right) & a+2\left(\frac{b-a}{x-1}\right) & \cdots & \underbrace{a+(x-1)\left(\frac{b-a}{x-1}\right)}_{\Large =\;b} \end{array}$$