3
\$\begingroup\$

This question already has an answer here:

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\$

marked as duplicate by Erik the Outgolfer, ბიმო, ASCII-only, Angs, Community Apr 15 '18 at 12:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ "it has to be human readable" is very subjective. Can you please elaborate on what you mean? \$\endgroup\$ – Erik the Outgolfer Apr 15 '18 at 11:46
  • 1
    \$\begingroup\$ @EriktheOutgolfer as a human, I don't mind reading a polynomial in list of coefficients format \$\endgroup\$ – Angs Apr 15 '18 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\$ – ბიმო Apr 15 '18 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\$ – ბიმო Apr 15 '18 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\$ – user202729 Apr 15 '18 at 12:09
1
\$\begingroup\$

Pari/GP, 28 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

InterpolatingPolynomial[##,x]&

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Not to mention that this does not use infix notation. :/ \$\endgroup\$ – user202729 Apr 15 '18 at 12:04
  • \$\begingroup\$ @user202729 does it need to? \$\endgroup\$ – ASCII-only Apr 15 '18 at 12:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.