Date: 16.06.
2011
Write algorithms to accomplish the following tasks:
1. Given n Boolean variables x1, x2, , and xn, we wish to print all possible combinations of truth values they can assume. For instance, if n=2, there are four possibilities: true, true; true, false; false, true: false, false. 2. Devise an algorithm that inputs three integers and outputs them in non decreasing order. 3. Present an algorithm that searches an unsorted array a[1:n] for the element x. If x occurs, then return a position in the array; else return zero. 4. The factorial function n! has value 1 when n 1 and value n * (n-1)! when n > 1 . Write both a recursive and an iterative algorithm to compute n!. 5. The Fibonacci numbers are defined as f 0 = 0, f1 = 1, and fi = fi 1 + fi 2 for i > 1. Write both a recursive and an iterative algorithm to compute f i .
6. Give an algorithm to solve the following problem: Given n, a positive integer, determine whether n is the sum of all of its divisors, that is whether n is the sum of all t such that 1 t n , and t divides n. 7. If S is a set of n elements, the powerset of S is the set of all possible subs ets of S. For example, if S= ( a, b, c), then the powerset (S) = {(a), (b), (c), (a,b), (b,c), (a,c), (a, b, c), }