![bm42400](/avatars/47313.jpg)
bm42400
25.09.2020 •
Computers and Technology
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions working for n equal to 40, determine which method is more efficient by timing the two separate function calls and printing out the time required for each method call to return the 40th element in the sequence. Return the 40th element to main and print it. After that, print out a complete Fibonacci sequence from element 1 to element 40, along with its position number. 1 1 2 3
Solved
Show answers
More tips
- H Health and Medicine How to Choose the Right Glasses?...
- H Health and Medicine How to Treat Whooping Cough in Children?...
- H Health and Medicine Simple Ways to Lower Cholesterol in the Blood: Tips and Tricks...
- O Other How to Choose the Best Answer to Your Question on The Grand Question ?...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
- S Style and Beauty Intimate Haircut: The Reasons, Popularity, and Risks...
- A Art and Culture When Will Eurovision 2011 Take Place?...
- S Style and Beauty How to Choose the Perfect Hair Straightener?...
- F Family and Home Why Having Pets at Home is Good for Your Health...
- H Health and Medicine How to perform artificial respiration?...
Answers on questions: Computers and Technology
- C Computers and Technology Write and algorithm to read and print your name...
- C Computers and Technology What are the earliest invention in human history...
- M Mathematics If ur 18 or older text me...
- W World Languages Fill in the blank with the word or phrase that best completes the sentence. Oedipodis nomen “ transfixus” significat. A) Filius B) Vir C) Pes D) Mons...
- M Mathematics Random samples are useful because they can represent the entire larger population. They re used often in polls that want to see what the whole country, for example, thinks about...
- M Mathematics Solve 8.2 w less-than-or-equal-to 29.52. Which of the following must be true about the inequality and the resulting graph? Select three options....
- E English Why can humans learn from a tale mostly about animals?...
- H History What is Obama’s last name?...
- H History 1. When did American Indians start farming?a. AD 1565b. AD 1607C. AD 200d. AD 1142...
- S Social Studies During the Zhou Dynasty (1027 to 221 B.C.E.), the rulers of China governed through local nobles who were granted land in exchange for military service. Peasants farmed the land...
Ответ:
Here is the program that contains a recursive and an iterative function to calculate the nth element in a Fibonacci sequence :
import java.util.Scanner; // to take input from user
public class Main {
public static void main(String[] args) { //start of main function
Scanner input = new Scanner(System.in); // creates Scanner class object
System.out.print("Enter the value of n: "); //prompts user to enter n
int n = input.nextInt(); // reads the input value of n from user
System.out.println("Fibonacci using iteration:"); // displays result of iterative method
long start = System.currentTimeMillis(); //computes the time required by iterative method
System.out.printf("Element at index %d = %d \n", n, Iteration(n)); //returns the nth element of fibonacci series
System.out.printf("Time taken: %d ms\n", System.currentTimeMillis() - start); //displays the time taken to return the nth element of fibonacci series using iterative method
int i = 0, j;
for ( j = 1 ; j <= n ; j++ ) { //used to print the fibonacci sequence using iterative method
System.out.printf("%d : %d\n",j, Iteration(i));
i++; }
System.out.println("Fibonacci using recursion:"); // displays result of recursive method
start = System.currentTimeMillis(); //computes the time required by recursive method
System.out.printf("Element at index %d = %d \n", n, Recursion(n)); //returns the nth element of fibonacci series
System.out.printf("Time: %d ms\n", System.currentTimeMillis() - start); //displays the time taken to return the nth element of fibonacci series using recursive method
int ir = 0, jr;
for ( jr = 1 ; jr <= n ; jr++ ) {
System.out.printf("%d : %d\n",jr, Iteration(ir)); //used to print the fibonacci sequence using recursive method
ir++; } }
static int Iteration(int n) { // iterative method for fibonacci series
int a = 0, b = 1, c = 1;
for (int i = 0; i < n; i++) {
a = b;
b = c;
c = a + b; }
return a; }
static int Recursion(int n) { // recursive method for fibonacci series
if ((n == 1) || (n == 0)) { //base case
return n; }
return Recursion(n - 1) + Recursion(n - 2); } } //recursive case
Explanation:
The explanation is provided in the attached document.
Ответ: