Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

88%
+13 −0
Challenges 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 va...

20 answers  ·  posted 3y ago by moony‭  ·  last activity 2y ago by south‭

Question code-golf math
#4: Post edited by user avatar user‭ · 2021-08-17T22:15:23Z (over 2 years ago)
Use [] because it wasn't clear where the list ended and where x was
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`<br/>
  • `1.0, 2.0`, `4` -> `9`<br/>
  • `2.5, 2.0`, `0.5` -> `3.5`<br/>
  • `1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25`<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
  • # 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`<br/>
  • `[1.0, 2.0]`, `4` -> `9`<br/>
  • `[2.5, 2.0]`, `0.5` -> `3.5`<br/>
  • `[1.0, 2.0, 3.0, 4.0]`, `1.5` -> `24.25`<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
#3: Post edited by user avatar moony‭ · 2020-11-15T07:02:51Z (over 3 years ago)
  • # 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`<br/>
  • `1.0, 2.0`, `4` -> `9`<br/>
  • `2.5, 2.0`, `0.5` -> `3.5`<br/>
  • `1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
  • # 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`<br/>
  • `1.0, 2.0`, `4` -> `9`<br/>
  • `2.5, 2.0`, `0.5` -> `3.5`<br/>
  • `1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25`<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
#2: Post edited by user avatar xnor‭ · 2020-11-13T23:10:23Z (over 3 years ago)
  • # 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`<br/>
  • `1.0, 2.0`, `4` -> `9`<br/>
  • `2.5, 2.0`, `0.5` -> `3.5`<br/>
  • `1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
  • # 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`<br/>
  • `1.0, 2.0`, `4` -> `9`<br/>
  • `2.5, 2.0`, `0.5` -> `3.5`<br/>
  • `1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25<br/>
  • # Example ungolfed program (Rust)
  • ```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;
  • }
  • ```
#1: Initial revision by user avatar moony‭ · 2020-11-13T22:53:25Z (over 3 years ago)
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`<br/>
`1.0, 2.0`, `4` -> `9`<br/>
`2.5, 2.0`, `0.5` -> `3.5`<br/>
`1.0, 2.0, 3.0, 4.0`, `1.5` -> `24.25<br/>

# Example ungolfed program (Rust)
```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;
}
```