How to resolve the algorithm Forward difference step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Forward difference step by step in the Wren programming language
Table of Contents
Problem Statement
Provide code that produces a list of numbers which is the nth order forward difference, given a non-negative integer (specifying the order) and a list of numbers.
The first-order forward difference of a list of numbers A is a new list B, where Bn = An+1 - An. List B should have one fewer element as a result. The second-order forward difference of A will be: The same as the first-order forward difference of B. That new list will have two fewer elements than A and one less than B. The goal of this task is to repeat this process up to the desired order. For a more formal description, see the related Mathworld article.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Forward difference step by step in the Wren programming language
Source code in the wren programming language
import "./fmt" for Fmt
var forwardDiff = Fn.new { |a, order|
if (order < 0) Fiber.abort("Order must be a non-negative integer.")
if (a.count == 0) return
Fmt.print(" 0: $5d", a)
if (a.count == 1) return
if (order == 0) return
for (o in 1..order) {
var b = List.filled(a.count-1, 0)
for (i in 0...b.count) b[i] = a[i+1] - a[i]
Fmt.print("$2d: $5d", o, b)
if (b.count == 1) return
a = b
}
}
forwardDiff.call([90, 47, 58, 29, 22, 32, 55, 5, 55, 73], 9)
You may also check:How to resolve the algorithm Factorial step by step in the Janet programming language
You may also check:How to resolve the algorithm Modified random distribution step by step in the C++ programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the PureBasic programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Yacas programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Kotlin programming language