3
\$\begingroup\$

Introduction

A sequence of numbers is passed in as the input. The program has to generate the lowest degree polynomial possible. This was my first programming project in college and it would be interesting to see how much smaller the solution could have been.

Challenge

The input is a list of integers separated by a comma. The output is simply the equation that generates that sequence. The exact output of the equation is up to you i.e. no spaces are required between terms of the polynomial but it has to be human readable. The equation has to be expressed with the variable x. The first integer in the supplied sequence will correspond with x = 1, the second integer supplied would be x = 2 etc.

i.e.

x^2

would result in the sequence

1,4,9,16 NOT 0,1,4,9

Example Input and Output

Input:

5,14,27,44

Output:

2x^2 + 3x

Edit

I marked the questions as duplicate, thanks for the head up.

\$\endgroup\$
15
  • 1
    \$\begingroup\$ "it has to be human readable" is very subjective. Can you please elaborate on what you mean? \$\endgroup\$ Commented Apr 15, 2018 at 11:46
  • 1
    \$\begingroup\$ @EriktheOutgolfer as a human, I don't mind reading a polynomial in list of coefficients format \$\endgroup\$ Commented Apr 15, 2018 at 11:49
  • 1
    \$\begingroup\$ Duplicates: this and with less restrictive I/O this. I don't think just supplying the vector [1..n] adds much to the existing challenges. \$\endgroup\$ Commented Apr 15, 2018 at 11:53
  • 1
    \$\begingroup\$ Now it's linked three times.. @ASCII-only: You should probably answer the other one and not this one if you think this is a duplicate.. \$\endgroup\$ Commented Apr 15, 2018 at 12:00
  • 1
    \$\begingroup\$ And have anyone mentioned that? Welcome to PPCG! Next time you can use the sandbox for proposed challenges. (note that because of lack of people visiting the sandbox it takes a while to get feedback) \$\endgroup\$ Commented Apr 15, 2018 at 12:09

2 Answers 2

1
\$\begingroup\$

Pari/GP, 28 bytes

l->polinterpolate([1..#l],l)

Try it online!

answered Apr 15, 2018 at 11:48
\$\endgroup\$
0
1
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

InterpolatingPolynomial[##,x]&

Try it online!

answered Apr 15, 2018 at 11:59
\$\endgroup\$
2
  • \$\begingroup\$ Not to mention that this does not use infix notation. :/ \$\endgroup\$ Commented Apr 15, 2018 at 12:04
  • \$\begingroup\$ @user202729 does it need to? \$\endgroup\$ Commented Apr 15, 2018 at 12:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.