How to resolve the algorithm Padovan n-step number sequences step by step in the Julia programming language
How to resolve the algorithm Padovan n-step number sequences step by step in the Julia 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 Julia programming language
Function nstep_Padovan
:
-
Purpose: Generates the first
nterms
terms of the firstmax_nstep
2..max_nstep-step Padovan sequences. -
Input Parameters:
max_nstep
: Maximum step value for the Padovan sequences (default: 8).nterms
: Number of terms to generate for each step value (default: 15).
-
Working:
- Initializes a list
start
with empty sequences for n=0 and [1, 1, 1] for n=1 (hidden). - Iterates from n=2 to
max_nstep
:- Initializes
this
with the lastn+1
terms from the previous step. - Extends
this
with new terms until it reachesnterms
. - Appends
this
to thestart
list.
- Initializes
- Initializes a list
-
Output: Returns a list containing the Padovan sequences for each step value from 2 to
max_nstep
.
Function print_Padovan_seq
:
-
Purpose: Prints the Padovan sequences in a table format.
-
Input Parameter:
p
: A list of Padovan sequences generated bynstep_Padovan
.
-
Working:
- Prints a table header with columns for
n
and "Values." - Iterates through the Padovan sequences in the list:
- Prints the step value
n
and the terms in the sequence, removing any non-numeric characters. - Adds a continuation mark (
...
) to indicate there may be more terms.
- Prints the step value
- Closes the table.
- Prints a table header with columns for
In the main program:
- Calls the
nstep_Padovan
function to generate the Padovan sequences. - Calls the
print_Padovan_seq
function to print the sequences in a table format.
Output:
- Prints a table containing the Padovan sequences for each step value from 2 to 8, with 15 terms per sequence. The table is formatted using CSS styles for alignment and coloring.
Source code in the julia programming language
"""
First nterms terms of the first 2..max_nstep -step Padovan sequences.
"""
function nstep_Padovan(max_nstep=8, nterms=15)
start = [[], [1, 1, 1]] # for n=0 and n=1 (hidden).
for n in 2:max_nstep
this = start[n][1:n+1] # Initialise from last
while length(this) < nterms
push!(this, sum(this[end - i] for i in 1:n))
end
push!(start, this)
end
return start[3:end]
end
function print_Padovan_seq(p)
println(strip("""
:::: {| style="text-align: left;" border="4" cellpadding="2" cellspacing="2"
|+ Padovan <math>n</math>-step sequences
|- style="background-color: rgb(255, 204, 255);"
! <math>n</math> !! Values
|-
"""))
for (n, seq) in enumerate(p)
println("| $n || $(replace(string(seq[2:end]), r"[ a-zA-Z\[\]]+" => "")), ...\n|-")
end
println("|}")
end
print_Padovan_seq(nstep_Padovan())
You may also check:How to resolve the algorithm SHA-256 Merkle tree step by step in the C++ programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Scheme programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Groovy programming language