How to resolve the algorithm Padovan n-step number sequences step by step in the C programming language
How to resolve the algorithm Padovan n-step number sequences step by step in the C 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 C programming language
Explanation of the Code:
This C program generates and prints the first few terms of the Padovan n-step number sequence for various values of n. The Padovan sequence is defined as follows:
P(n) = 1, for n = 0 or 1
P(n) = P(n-2) + P(n-3), for n >= 2
Function padovanN
:
- This function generates the Padovan n-step number sequence up to the specified term
t
. - It takes three parameters:
n
(the number of steps, where n >= 2),t
(the number of terms to generate), andp
(an array to store the sequence). - If
n
is less than 2 ort
is less than 3, it initializes all elements ofp
to 1 and returns. - Otherwise, it recursively calls itself with
n-1
to generate the Padovan sequence for the previous step. - It then calculates the terms from
P(n+1)
toP(t)
using the recurrence relation:P(i) = P(i-2) + P(i-3) + ... + P(i-n-1), for i >= n+1
Function main
:
- This is the entry point of the program.
- It defines a constant
t
(number of terms to generate) and an arrayp
to store the sequence. - It iterates over different values of
n
(from 2 to 8) and generates the Padovan sequence for eachn
usingpadovanN
. - It prints the generated sequence for each
n
.
Output:
The program prints the first 15 terms of the Padovan sequence for n
values from 2 to 8:
First 15 terms of the Padovan n-step number sequences:
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
4: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
5: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
6: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
7: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
8: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
Note that the sequence is the same for all n
values greater than 1, because the recurrence relation only depends on the previous two terms.
Source code in the c programming language
#include <stdio.h>
void padovanN(int n, size_t t, int *p) {
int i, j;
if (n < 2 || t < 3) {
for (i = 0; i < t; ++i) p[i] = 1;
return;
}
padovanN(n-1, t, p);
for (i = n + 1; i < t; ++i) {
p[i] = 0;
for (j = i - 2; j >= i - n - 1; --j) p[i] += p[j];
}
}
int main() {
int n, i;
const size_t t = 15;
int p[t];
printf("First %ld terms of the Padovan n-step number sequences:\n", t);
for (n = 2; n <= 8; ++n) {
for (i = 0; i < t; ++i) p[i] = 0;
padovanN(n, t, p);
printf("%d: ", n);
for (i = 0; i < t; ++i) printf("%3d ", p[i]);
printf("\n");
}
return 0;
}
You may also check:How to resolve the algorithm Middle three digits step by step in the Forth programming language
You may also check:How to resolve the algorithm Anagrams step by step in the SuperCollider programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Sidef programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Julia programming language
You may also check:How to resolve the algorithm Variable declaration reset step by step in the FreeBASIC programming language