Example: Find the maximum of three numbers, a, b, and c
Example: Algorithm max which finds the maximum of three numbers, a, b, and c.
Input: Three numbers a, b, c
Output: A number which is the
largest of the tree input numbers
procedure max(a, b, c) 1. x := a // x is a local/temporary variable 2. if b > x then 3. x := b // update x 4. if c > x then 5. x := c // update x 6. return x
Input: A list of integers s =
{s1,..,sn}, and the length n of the
list
Output: A number which is the largest in s.
1. procedure find_large(s,n) 2. large := s1 3. i := 2 4. while i £ n do 5. begin 6. if si > large then // a larger value was found 7. large := si 8. i := i + 1 ; 9. end 10. return(large) 11. end find_large
Input: n (an integer), and d (a positive
integer)
Output: q and r (integers)
1.procedure Div(n, d, q, r) 2. q := 0 r := n // initialize q and r 3. while r ³ d do 4. begin 5. q := q + 1 r := r - d 6. end 7. return(q) 8. end Div // remainder can be found from variable "r"
Input: A positive integer m
Output:
true if m is even; false if m is odd
1. procedure is_evern(m) 2. if m mod 2 = 0 then return (true) 3. else return (false) 4. end is_even
Input: A positive integer m
Output:
true if m is prime; false if m is not prime
1. procedure is_prime(m) 2. for i := 2 to m-1 do 3. if m mod i = 0 then return (false) 3. return (true) 4. end is_prime
Example: gcd(30, 105) = 15
divisor of 30 | 2 X 3 X 5 |
divisor of 105 | 3 X 5 X 7 |
common | 3 X 5 |
Examples: 15 | 30, and the quotient is 2.
Let m, n and c be integers.
First, let's check some cases:
Proof:
Let C1 be the set of common divisors of a and b, and C2 be the set of common divisors of b and r. We show C1 Ê C2, and C2 Ê C1. Then we can conclude C1 º C2.
1) Show C1 Ê
C2
Let c Î
C1 be a common divisor of a and b, that is, c | a and c |
b.
From the third property of divisors, we get that c |
bq.
Then from the second property of divisors, we get that c |
(a - bq) = r. ... (A)
From (A) and hypothesis, we have that c | b and c |
r. Therefore, c is a common divisor of b and r, that is, c Î
C2.
2) Show C2 Ê
C1
Let c Î
C2 be a common divisor of b and r, that is, c | b and c |
r.
From the third property of divisors, we get that c |
bq.
Then from the first property of divisors, we get that c | (
bq + r) =a. ... (B)
From (B) and hypotehsis, c | a and c | b.
Therefore, c is a common divisor of a and b, that is, c Î
C1.
From 1) and 2), we showed that all common divisor of a and b are also common divisors of b and r. In particular, this is true for the greatest common divisor, i.e., gcd(a, b) = gcd(b, r).
Algorithm
procedure gcd(a, b) 1. if a < b, then 2. swap(a, b) // make a the larger of the two 3. while b != 0 do 4. begin 5. r := a mod b 6. a := b 7. b := r 8. end 9. return a
Example 1 : gcd(105,30)
iteration | a | b | r | |
1 | gcd(105, 30) | 105 | 30 | 15 |
2 | 30 | 15 | 0 | |
3 | 15 | 0 |
iteration | a | b | r | |
1 | 273 | 110 | 53 | |
2 | 110 | 53 | 4 | |
3 | 53 | 4 | 1 | |
3 | 4 | 1 | 0 |
A recursive procedure is a procedure that invokes itself.
A recursive algorithm is an algorithm that contains a recursive procedure .
Examples
Recursive algorithm computes n!
1. procedure factorial(n) 2. if n = 0 then 3. return (1) 4. return (n*factorial(n-1)) 5. end factorial
Recursive Euclidean Algorithm
1. procedure gcd(a, b) 2. if a < b, then 3. swap(a, b) // make a the larger of the two 4. if b = 0 then 5. return(a) 6. r := a mod b 7. return(gdc(b,r)) 8. end gcd
Recursive Algorithm computes the n-th Fibonacci number
1. procedure fib(n) 2. if n £ 2 then 3. return n 4. else return(fib(n-1) + fib(n-2)) 5. end fib
1. procedure fib(n) // assume n ³ 1 2. if n £ 2 then 3. return n 4. f1 := 1 f2 := 2 f3 := f1 + f2 i := 3 // initialize f1 = 1 and f2 = 2 5. while i < n do 4. begin 5. f1 := f2 f2 := f3 f3 := f1 + f2 i := i + 1 6. end 7. return(f3) 8. end fib