The Leonardo numbers are a sequence of integers defined by the following recurre
ID: 3770901 • Letter: T
Question
The Leonardo numbers are a sequence of integers defined by the following recurrence:
L (0) = 1
L (1) = 1
L (n ) = L (n 1 ) + L ( n 2 ) + 1
The first few Leonardo numbers are 1, 1, 3, 5, 9, 15, 25.
The Leonardo number problem is:
input: a non-negative integer n
output: Leonardo number L(n )
1) Design an algorithm for this problem using the dynamic programming pattern. Write pseudocode for your algorithm below. If you write multiple drafts, circle your final draft.
2) then, Prove the efficiency class of your algorithm using any method you wish.
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LeonardoNumbers {
public static void main(String[] args) throws NumberFormatException, IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter the range to which leonard numbers should be printed");
int n = Integer.parseInt(br.readLine());
LeonardoNumbers lnd = new LeonardoNumbers();
System.out.println("Leonardo Series is as follows ");
int result = 0;
for(int i = 1;i<=n; i++){
result = lnd.leonardo(i);
System.out.print(" "+result);
}
}
private int leonardo(int n) {
if(n == 1)
return 1;
if(n == 2)
return 1;
else
return leonardo(n-1)+leonardo(n-2) + 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.