0% found this document useful (0 votes)
6 views1 page

Recursive Functions in Python: GCD & Games

The document contains 3 programming questions: 1. Write a recursive function called GCD that calculates the greatest common divisor of two integers using the principle that GCD(a, b) = GCD(b, r) where r is the remainder of a divided by b. 2. Write a program that plays a "guess the number" game by randomly selecting a number between 1-1000 and having the user guess it, providing feedback if the guess is too high, too low, or correct. 3. Write a recursive function (bonus question) that determines if a given input number is prime or not.

Uploaded by

Hasan Alomari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Recursive Functions in Python: GCD & Games

The document contains 3 programming questions: 1. Write a recursive function called GCD that calculates the greatest common divisor of two integers using the principle that GCD(a, b) = GCD(b, r) where r is the remainder of a divided by b. 2. Write a program that plays a "guess the number" game by randomly selecting a number between 1-1000 and having the user guess it, providing feedback if the guess is too high, too low, or correct. 3. Write a recursive function (bonus question) that determines if a given input number is prime or not.

Uploaded by

Hasan Alomari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Task #2

Question#1:
Write a recursion function named GCD that computes the greatest common divisor of two integers.
Based on the observation that, if r is the remainder when a is divided by b, then the common divisors of
a and b are the same as the common divisors of b and r. Thus we can use the equation: GCD (a, b) =
GCD (b, r) .
For example, GCD (36, 20) = GCD (20, 16) = GCD (16, 4) = GCD (4, 0) = 4 implies that the GCD of
36 and 20 is 4. It can be shown that for any two starting numbers, this repeated reduction eventually
produces a pair where the second number is 0. Then the GCD is the other number in the pair.

Question#2:
(Guess the Number Game) Write a program that plays the game of "guess the number" as follows: Your
program chooses the number to be guessed by selecting an integer at random in the range 1 to 10. The
program then displays the following:
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.

The player then types a first guess. The program responds with one of the following:
1.
2.
3.

Excellent! You guessed the number!


Would you like to play again (y or n)?
Too low. Try again.
Too high. Try again.

Question#3 (Bonus):
Write A recursive function to determine if an input is prime number or not.

You might also like