Let's say I have these 7 natural numbers (all between 0 and 255):

255, 23, 45, 32, 87, 52, 146

How can I find a function F(x) that, once computed, gives me back these values, ie.

f(1) = 255
f(2) = 23
f(3) = 45

I'm not sure what's the technical term to use for this, but I trust you got the picture.

All the above numbers are just examples (except that all values are natural number between 0 and 255).

All I really care is that I need the simplest possible function that can give me such result, be it polynomial, quartic or any other type of function.

I used to be good at math, but it's been at least 10 years since I took my last math class, so please bear with me!

Thanks in advance!

  • 1
    $\begingroup$ Lagrange interpolation provides a nice general polynomial answer. However, suppose that $f(1)=2$, $f(2)=4$, $f(3)=8$, and so on. Then $f(n)=2^n$ is a simple formula, while the Lagrange interpolation polynomial is fairly messy. $\endgroup$ Mar 17, 2012 at 17:50

1 Answer 1


Maybe you want to use Lagrange interpolation. This formula gives the polynomial with the smallest degree that fits your data points exactly. You can find the formula for the Lagrange polynomial at the link, and also there are online calculators that will calculate it for you.

For instance, when I plug your data points [1, 255], [2,23], [3,45], [4,32], [5,87], [6,52], [7,146] into this calculator, I get back the result:

$$f(x)={1291x^6-31029x^5+296455x^4-1434195x^3+3689254x^2-4761696x+2423520\over 720}$$


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .