How to resolve the algorithm Leonardo numbers step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Leonardo numbers step by step in the C++ programming language
Table of Contents
Problem Statement
Leonardo numbers are also known as the Leonardo series.
The Leonardo numbers are a sequence of numbers defined by:
This task will be using the 3rd equation (above) to calculate the Leonardo numbers.
Edsger W. Dijkstra used Leonardo numbers as an integral part of his smoothsort algorithm.
The first few Leonardo numbers are:
(The last task requirement will produce the Fibonacci numbers.)
Show all output here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Leonardo numbers step by step in the C++ programming language
This C++ code generates and prints the first 25 numbers of two mathematical series: the Leonardo numbers and the Fibonacci numbers. Here's a breakdown of the code:
-
Header and Namespace:
- The code includes the necessary header file
<iostream>
for input and output operations. - It uses the
std
namespace, which contains standard C++ library functions and objects.
- The code includes the necessary header file
-
Function leoN:
- This function generates and prints a specified number of terms of a mathematical series.
- It takes several parameters:
cnt
: The number of terms to generate.l0
(default 1): The initial value of the series.l1
(default 1): The second initial value of the series.add
(default 1): An additional value to add in each iteration for the Leonardo series only.
- The function uses a loop to calculate and print the terms of the series:
- For each iteration (from 0 to
cnt
-1), it prints the current value ofl0
. - It calculates the next term of the series by adding
l0
andl1
(andadd
for the Leonardo series). - It updates
l0
to the previous value ofl1
and updatesl1
to the newly calculated next term.
- For each iteration (from 0 to
-
Main Function:
- This is the entry point of the program.
- It invokes the
leoN
function twice:- First, it prints the first 25 Leonardo numbers by calling
leoN(25)
with its default parameters. - Next, it prints the first 25 Fibonacci numbers by calling
leoN(25, 0, 1, 0)
. Here,add
is set to 0 to generate Fibonacci numbers because in the Fibonacci series, the next term is the sum of the previous two terms.
- First, it prints the first 25 Leonardo numbers by calling
-
Output:
- The program generates and prints the first 25 terms of both the Leonardo and Fibonacci series, separated by spaces.
Source code in the cpp programming language
#include <iostream>
void leoN( int cnt, int l0 = 1, int l1 = 1, int add = 1 ) {
int t;
for( int i = 0; i < cnt; i++ ) {
std::cout << l0 << " ";
t = l0 + l1 + add; l0 = l1; l1 = t;
}
}
int main( int argc, char* argv[] ) {
std::cout << "Leonardo Numbers: "; leoN( 25 );
std::cout << "\n\nFibonacci Numbers: "; leoN( 25, 0, 1, 0 );
return 0;
}
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Phix programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the Python programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the C# programming language
You may also check:How to resolve the algorithm Range extraction step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the ReScript programming language