Download Lab 8 (10 points) Please sign in the sheet and submit the

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of prime numbers wikipedia , lookup

Addition wikipedia , lookup

History of mathematics wikipedia , lookup

Recurrence relation wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Patterns in nature wikipedia , lookup

Transcript
Lab 8 (10 points)
Please sign in the sheet and submit the assignment in Morbius:
In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci.
The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the
previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. The Fibonacci series may be
defined recursively as follows:
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(2) = fibonacci(0) + fibonacci(1) = 0+1 = 1
fibonacci(3) = fibonacci(1) + fibonacci(2) = 1+1 = 2
fibonacci(4) = fibonacci(2) + fibonacci(3) = 1+2 = 3
..
..
fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)
Your task is to:
Task 1. (4 points): Implement the Fibonacci number iteratively: Write a class which has a method that can take an
integer input. This input indicates which Fibonacci number to generate. As input provide 10. And as output just print
Fibonacci number 10. A sample input/output is given below. Here system prompt is shown as italic text. User input is in red
color and program is in bold text.
Please enter which Fibonacci number you want to generate:
10
Fibonacci of 10 is: 55
Task 2. (6 points): Implement the Fibonacci number with recursion: Write a class which has a recursive method that
can take an integer input. This input indicates up to which Fibonacci number to generate. As input provide 10. And as just
print from Fibonacci number 0 to Fibonacci number 10. A sample input/output is given below. Here system prompt is
shown as italic text. User input is in red color and program is in bold text.
Please enter Upto which Fibonacci number you want to generate:
10
Here is the output:
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55