Comments on Evaluate a single variable polynomial equation
Post
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 …
4y ago
Japt `-x`, 6 5 bytes ËV …
4y ago
Ruby, 50 bytes ```ruby def …
4y ago
[Haskell], 20 bytes …
4y ago
Ruby, 38 bytes Simple map and …
4y ago
[Raku], 19 bytes (Z …
4y ago
Japt, 12 bytes ÊÆgX VpX …
4y ago
[JavaScript (Node.js)], 40 byt …
4y ago
Vyxal, 6, 5, 4 bytes ``` Źe …
4y ago
[Jelly], 1 byte ḅ Tr …
4y ago
[Python 3], 46 bytes …
3y ago
JavaScript (Node.js), 33 bytes …
3y ago
Pyth, 10 bytes sR^HhZhA …
4y ago
[Python 3], 46 bytes ```pyt …
3y ago
[C++ (gcc)], 61 bytes …
3y ago
Scala, 18 bytes ``` x=>.:\(. …
3y ago
J, 2 bytes ```J p. ``` …
3y ago
Ruby, 29 bytes ```ruby ->l …
3y ago
BQN, 13 bytes ```bqn {+´(𝕨 …
3y ago
[Python 3], 167 127 118 117 94 …
3y ago
2 comment threads