Function for number patterns

I'm primarily a programmer and am pretty rusty at math... so forgive me if this is a stupid question.

I have four number patterns as follows:

• a) 3, 13, 31
• b) 5, 17, 37
• c) 7, 21, 43
• d) 9, 25, 49

What are the functions to get any number (n) for equation (abcd)?

I know the pattern, and how to brute-force it, but I'm lost when it comes to knowing the exact formula to get, say, the 1000th value in the pattern. The pattern is very related in all the equations.

The pattern I know is this:

Every successive value increases by 8 more than the last value increased by, except for the first value which is increased by (7 + first value).

• For instance, 3 in pattern 1 is increased by (7 + 3): 3 + 10 = 13
• 13 is then increased by 8 more than 10: 13 + 18 = 31
• 31 is then increased by 8 more than 18: 31 + 26 = 57 making a(4) = 57

Now how do I convert this knowledge into a function so that I don't have to write a programmatic loop every time I want to find x(n)?

-
Do you want the function itself or how to figure out how to get the function if, say the rules change a little? –  Mitch Aug 31 '11 at 21:07
Both preferably... –  nzifnab Aug 31 '11 at 21:08
Since the function's second forward difference $\Delta^2 f$ is constant, $f(n)$ will be quadratic as $an^2+bn+c$. Write $f(n)=m$ for $n=1,2,3$ and $m$ the given values and you get a $3\times3$ linear system solvable with a simple matrix inversion. –  anon Aug 31 '11 at 21:11
There's not enough information. Where is the data from? –  Qiaochu Yuan Aug 31 '11 at 21:17
I feel that I've provided plenty of data. For the first number pattern the first 10 numbers are: 1, 3, 13, 31, 57, 91, 133, 183, 241, 307 I gave you the way I generate the numbers, using a programmatic loop that iterates n times. –  nzifnab Aug 31 '11 at 21:22
The first forward difference is defined by $\Delta f = f(n+1)-f(n)$. According to the intended pattern, this difference increases by $8$ every term. In the first instance, you get $$\Delta f (1)=13-3=10$$ $$\Delta f(2)=31-13=10+8.$$ It's easy to see that $\Delta f(3)=10+8+8$ and so on, so we get that $\Delta f(n)=2+8n$. Note also that $$\Delta f(1)+\Delta f(2)+\cdots+\Delta f(n-1)$$ is a telescoping sum which, after cancellation, evaluates to $f(n)-f(1)$. Hence $$f(n)=f(1)+\sum_{k=1}^{n-1}\Delta f(k)$$ $$=3+\sum_{k=1}^{n-1}(2+8k)$$ $$=3+2(n-1)+8\frac{(n-1)n}{2}$$ $$=4n^2-2n+1.$$ Above we used the formula for triangle number sums. This quadratic function agrees with the given values for $n=1,2,$ and $3$. This method can be applied to the other three patterns with
$$\Delta f_2(n)=4+8n, f_2(1)=5$$ $$\Delta f_3(n)=6+8n, f_3(1)=7$$ $$\Delta f_4(n)=8+8n, f_4(1)=9.$$
However, for the last pattern $f_4$, you should be able to see the terms as $3^2,5^2,7^2$ so that the general term is simply $f_4(n)=(2n+1)^2$.