Indksi Mat 2
Indksi Mat 2
Indksi Mat 2
An illustration
Penjumlahan n buah bilangan ganjil pertama 1=1 1+3=4 1+3+5=9 1 + 3 + 5 + 7 = 16 . . (2.1-1) = 12 1 + (2.2-1) = 22 1 + 3 + (2.3-1) = 32 1 + 3 + 5 + (2.4-1) = 42
RECURSIVE DEFINITIONS
Recursively defined functions To define a function with the set of nonnegative integers as its domain, 1. Specify the value of the function at zero. 2. Give a rule for finding its value as an integer from its values at smaller integer.
Examples:
1 Suppose f(0) = 3 and f(n+1) = 2.f(n)+3. Find f(1), f(2), f(3), and f(4). 2 If f(0)=0, f(1)=1 and f(n)=f(n-1)+f(n-2). Then f(6)? 3 Give a recursive definition of an where a is a nonzero real number and n is a nonnegative integer.
Recursive Algorithms
Definition. An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
Examples:
1.Algorithm for computing an where a is a nonzero real number and n is a nonnegative integer.
Procedure power(a:nonzero real number, n:nonnegative integer) if n=0 then power(a,n):=1 else power(a,n):=a*power(a,n-1)
2.Linier search algorithm as a recursive procedure Procedure search(i,j,x) if ai=x then location:=i else if i = j then location:=0 else search(i+1,j,x)
Procedure recursive factorial(n: positive integer) if n=1 then factorial(n):= 1 else factorial(n):= n*factorial(n-1)
An Iterative Procedure for Factorial Procedure iterative factorial(n: positive integer) x:= 1 for i:= 1 to n x:= i*x {x is n!}