How to resolve the algorithm Padovan n-step number sequences step by step in the Go programming language
How to resolve the algorithm Padovan n-step number sequences step by step in the Go programming language
Table of Contents
Problem Statement
As the Fibonacci sequence expands to the Fibonacci n-step number sequences; We similarly expand the Padovan sequence to form these Padovan n-step number sequences. The Fibonacci-like sequences can be defined like this: For this task we similarly define terms of the first 2..n-step Padovan sequences as: The initial values of the sequences are:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Padovan n-step number sequences step by step in the Go programming language
This program is written in Go and it implements the Padovan sequence, which is a sequence of numbers where each number is the sum of the previous two numbers, with the first two numbers being 1 and 1.
The program defines a function padovanN
that takes two integers as arguments: n
is the number of steps in the sequence, and t
is the total number of terms to be generated.
The function padovanN
creates a slice of integers of length t
and initializes it with 1s.
Then, for each value of n
from 2 to 8, it calls the padovanN
function recursively to generate the first n
terms of the sequence.
For each term i
greater than n
, it sets the value of p[i]
to 0 and then loops through the previous n
terms, adding their values to p[i]
.
Finally, the function returns the slice p
.
The main function calls the padovanN
function for each value of n
from 2 to 8 and prints the first t
terms of the sequence.
Here is an example of the output of the program:
First 15 terms of the Padovan n-step number sequences:
2: 1
3: 1
4: 1
5: 2
6: 2
7: 3
8: 4
Source code in the go programming language
package main
import "fmt"
func padovanN(n, t int) []int {
if n < 2 || t < 3 {
ones := make([]int, t)
for i := 0; i < t; i++ {
ones[i] = 1
}
return ones
}
p := padovanN(n-1, t)
for i := n + 1; i < t; i++ {
p[i] = 0
for j := i - 2; j >= i-n-1; j-- {
p[i] += p[j]
}
}
return p
}
func main() {
t := 15
fmt.Println("First", t, "terms of the Padovan n-step number sequences:")
for n := 2; n <= 8; n++ {
fmt.Printf("%d: %3d\n", n, padovanN(n, t))
}
}
You may also check:How to resolve the algorithm Sierpinski pentagon step by step in the Julia programming language
You may also check:How to resolve the algorithm Hilbert curve step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the Ruby programming language
You may also check:How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the Wren programming language