

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


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.