# Evaluate a single variable polynomial equation

# Challenge

Given a list of n numbers and x, compute $a + bx^1 + cx^{2} + ... + zx^{n-1}$, where a is the first value in the list, b is the second, etc. n is at most 256 and at least 0. The input value(s) can be any 32-bit float

Input can be in any format of choice, as long as it is a list of numbers and x. (And this'll likely stay this way, even if input rules change over time)

# Test inputs

`1.0`

, `182`

-> `1`

`1.0, 2.0`

, `4`

-> `9`

`2.5, 2.0`

, `0.5`

-> `3.5`

`1.0, 2.0, 3.0, 4.0`

, `1.5`

-> `24.25`

# Example ungolfed program (Rust)

```
// dbg! is a logging function, prints the expression and it's output.
// Good for seeing what's happening
// Test setup
pub fn main() {
let inp: &[f32] = &[1.0, 2.0, 3.0, 4.0];
let x: f32 = 1.5;
dbg!(evaluate_polynomial(inp, x)); // take inputs, print result
}
// Actual challenge answer function
pub fn evaluate_polynomial(inp: &[f32], x: f32) -> f32 {
let mut accum: f32 = 0.0;
for (idx, val) in inp.iter().enumerate() {
// x.pow(idx) * val
accum += dbg!(x.powf(idx as f32) * val);
}
return accum;
}
```

[APL (Dyalog Unicode)], 11 3 1 …

9mo ago

Japt `-x`, 6 5 bytes ËV …

9mo ago

Japt, 12 bytes ÊÆgX VpX …

9mo ago

Ruby, 50 bytes ```ruby def …

9mo ago

Ruby, 38 bytes Simple map and …

7mo ago

[Raku], 19 bytes (Z …

9mo ago

[JavaScript (Node.js)], 40 byt …

9mo ago

Vyxal, 6, 5, 4 bytes ``` Źe …

7mo ago

[Haskell], 20 bytes …

9mo ago

[Jelly], 1 byte ḅ Tr …

7mo ago

JavaScript (Node.js), 33 bytes …

~2mo ago

Pyth, 10 bytes sR^HhZhA …

6mo ago

## 12 answers

#
APL (Dyalog Unicode), ^{11} ^{3} 1 byte

```
⊥
```

Anyone who can golf this further gets a cookie!

Function submission which takes reversed coefficients as right argument and $x$ as left argument.

-8 bytes from dzaima and rak1507(APL Orchard).

-2 bytes from Adám.

Uses a mixed base conversion.

#### 0 comment threads

# Ruby, 50 bytes

```
def f(k,x)k.length>1?k[0]+f(k[1..-1],x)*x:k[-1]end
```

This uses the Horner's method recursively, because I think it'll be slightly shorter than using a loop or builtin array functions.

Also, this is my first post on this website ... er my first real attempt at golfing something in Ruby, so please feel free to suggest ways to shorten my solution.

#### 0 comment threads

#
Vyxal, ~~6~~, ~~5~~, 4 bytes

```
Źe*∑
```

Takes input in the format `coeffs, x`

## Explained

```
Źe*∑
Ź # Generate range [0, len(coeffs))
e # Calculate x ** [0, len(coeffs) (vectorising)
* # Multiply the coefficients by the exponated x's. The lists are extended with 0s to be the same length
∑ # Sum that list and output
```

#### 0 comment threads

# Raku, 19 bytes

```
(*Z*(*X**0..*)).sum
```

Is it concerning that my solution is over 30% asterisks?

### Explanation

```
( ).sum # Get the sum of
*Z*( ) # The input list zip multiplied by
*X** # The second input to the power of
0..* # 0 to infinity
```

An alternate curried solution for 19 bytes is:

```
{*.reduce(*×$_+*)}
```

#### 0 comment threads

# Haskell, 20 bytes

```
f x=foldl((+).(x*))0
```

Takes input coefficients from highest degree to lowest.

**21 bytes**

```
x%(h:t)=h+x*x%t
x%_=0
```

#### 0 comment threads

# Jelly, 1 byte

```
ḅ
```

Essentially just Razetime's APL answer, except in that `ḅ`

vectorizes rather than carrying out mixed base conversion--irrelevant if, as is the case here, the provided base is scalar. Takes a reversed coefficient list on the left and $x$ on the right.

#### 0 comment threads

# JavaScript (Node.js), 33 bytes

```
a=>n=>a.reduce((o,x,y)=>o+x*n**y)
```

## 1 comment thread