Fibonacci Sequence and Greatest Common Divisor, Lecture notes of Computer Programming

Two programming questions. The first question asks to write a program to calculate the fibonacci sequence from the 10th number to the 100th number. The second question asks to write a recursive function to find the greatest common divisor (gcd) of two integers using the euclidean algorithm. Both questions include explanations and examples.

Typology: Lecture notes

2012/2013

Uploaded on 04/27/2013

kid
kid 🇮🇳

4.3

(18)

110 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Question 1
The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21.......,
where the first two terms are 0 and 1, and each term thereafter is the sum of
the two preceding terms; that is
fibonacci(0) = 0
fibonacci(1) =1
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
Using this information, write a program that calculate from the 10th number
to the 100th number in a Fibonacci sequence. For example, if n = 6, the
program should display the value 5.
Question 2
pf2

Partial preview of the text

Download Fibonacci Sequence and Greatest Common Divisor and more Lecture notes Computer Programming in PDF only on Docsity!

Question 1

The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21......., where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms; that is fibonacci(0) = 0 fibonacci(1) = fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Using this information, write a program that calculate from the 10th number to the 100th number in a Fibonacci sequence. For example, if n = 6, the program should display the value 5.

Question 2

The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: if y is equal to 0, then gcd(x, y) is x; otherwise gcd(x, y) is gcd(y, x%y), where % is the modulus operator.