Generate lowest degree polynomial from sequence

3

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.

Joris Guex

Posted 2018-04-15T11:39:13.403

Reputation: 31

Question was closed 2018-04-15T12:05:43.630

Related, – alephalpha – 2018-04-15T11:46:10.673

1"it has to be human readable" is very subjective. Can you please elaborate on what you mean? – Erik the Outgolfer – 2018-04-15T11:46:56.080

Related. – user202729 – 2018-04-15T11:47:53.707

1@EriktheOutgolfer as a human, I don't mind reading a polynomial in list of coefficients format – Angs – 2018-04-15T11:49:36.003

@EriktheOutgolfer as long as another program is not required to understand the output, it's fine. For example, 3x^2 - 3x + 6 could be written as (3,2,-3,1,6,0) as long as you mention how the output is formatted in your post. – Joris Guex – 2018-04-15T11:50:47.567

1

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.

– ბიმო – 2018-04-15T11:53:13.173

@JorisGuex Sorry, but "as long as another program is not required to understand the output" is still very subjective, since then you can just output the input exactly and do the hard work yourself as a human to understand what the output really is. – Erik the Outgolfer – 2018-04-15T11:53:16.043

@BMO (Linked right above. Whether people would VTC is up to them.) – user202729 – 2018-04-15T11:54:15.807

@EriktheOutgolfer That's a fair point. Let me try to be more specific. The coefficient and exponent of each term in the polynomial has to be output in order from largest to smallest to avoid any ambiguity. For example, 2 + 4x^2 - 6x would have to be output as 4,2,-6,1,2,0 making sure the coefficient comes before the exponent for each term. No calculations should be needed to get the final answer – Joris Guex – 2018-04-15T11:55:26.653

@user202729: Ah I missed those, but I voted to close and left a comment justifying my decision. – ბიმო – 2018-04-15T11:59:01.017

1Now 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.. – ბიმო – 2018-04-15T12:00:52.003

@BMO that's a close vote auto-comment – ASCII-only – 2018-04-15T12:03:31.777

@BMO Also the VTC was after the answer. I guess ASCII-only only read your comment after posting the answer. – user202729 – 2018-04-15T12:05:57.567

1

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)

– user202729 – 2018-04-15T12:09:23.013

@user202729: That's not preventing them from answering the other one or at least deleting this one. – ბიმო – 2018-04-15T12:10:27.400

Answers

1

Pari/GP, 28 bytes

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

Try it online!

alephalpha

Posted 2018-04-15T11:39:13.403

Reputation: 23 988

1

Wolfram Language (Mathematica), 30 bytes

InterpolatingPolynomial[##,x]&

Try it online!

ASCII-only

Posted 2018-04-15T11:39:13.403

Reputation: 4 687

Not to mention that this does not use infix notation. :/ – user202729 – 2018-04-15T12:04:25.133

@user202729 does it need to? – ASCII-only – 2018-04-15T12:40:57.263