DAA Lab01
DAA Lab01
Problems
5
3
4
Problems
Problem
Algorithm
ALGORITHM ConsecutiveIntegerChecking(m, n)
//Computes gcd(m, n) by Consecutive Integer Checking Algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
t ← min(m, n)
while t > 0 do
if m mod t = 0 and n mod t = 0 then
break
t←t-1
return t
Consecutive Integer Checking Algorithm: Structured Description
ALGORITHM Euclid(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ← m mod n
m←n
n←r
return m
Euclid’s Algorithm: Pseudocode
Time Efficiency
Space Efficiency