lab1-kgashok.py

Table of Contents

Lab 1: Compute the gcd of two numbers

[TOC]

Problem statement

Write a Python program to compute the greatest common divisor (gcd) of two positive integers.

Solution Key

CloudCoder Exercise

http://cloudcoder.kgkite.ac.in/cloudcoder/#exercise?c=80,p=6941

  • How to find the gcd of two numbers using prime factorization? http://j.mp/gcdPrime

  • Why 1 is the common factor? http://j.mp/gcdOne

Pre-Lab Questions

  1. Show your manual workings for the gcd of 924 and 2562.

  2. What is the practical use of calculating the gcd of two numbers?

  3. If you have written the recursive solution of the Euclid algorithm, then write the iterative solution, and vice versa.

  4. How does the algorithm for the gcd of two numbers work? What is the name for this algorithm? Why is it so special?

  5. Why is the global keyword used in the function get_twonumbers?

  6. Is the function get_twonumbers() fruitful or not fruitful?

  7. Define a function is_valid_year with parameter year. The function must return True if the value of year is between 1900 and 3000 (inclusive). Otherwise, it must return False.

Post-lab Questions

  1. What if a or/and b are negative integers? How will you modify the program to handle this? Clue: Use the abs() function.

Bonus 1

  1. How will find the gcd of three integers?

  2. How do you calculate the number of days in a month?

PPT Slides showing the Recursive Calls

http://j.mp/gcdDemo

Recursion vs Iteration

recursionImage

Days per month

Last updated