Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Introduction Function in R Language
Function is a small program segment and which
can perform a specific task. It means for a particular task we can write a function. The advantage of function is we can develop the function independently. We develop a standalone function and then we can save it for future call/use. (1) A function can call another function. Suppose func1() calls func2(), then func2() calls func3() and so on. Normally while calling a function we maintain rule which is called as Master-Slave relationship. The function call should be always in one way. (2) A function may return a value by a statement called as return(value). (3) We can call a function as follows: y=sum(n). Here sum() is a function and we call it with some input data say n. The returned value will be stored in ‘y’ from where we call the function. Calling_function callee_function(). (4) A function can return only one value at a time. (5) In R language we can implement recursive call which means a function can call itself. In recursive call function1() can call function1() itself. Example on Recursive Call: Sum(n)=1+2+3+…+n where ‘n’ is any +ve integer. We choose n=5 and we calculate sum(5) using recursive call. Sum(5)=(5+4+3+2+1) =((5)+(4+3+2+1)) =((5)+sum(4)) =((5)+((4)+sum(3))) =((5)+((4)+((3)+sum(2)))) =((5)+((4)+((3)+((2)+sum(1))))) [Sum(1) will return (1) to calling function sum(2) =((5)+((4)+((3)+((2)+(1))))) =((5)+((4)+((3)+((3)))) = ((5)+((4)+((3)+((3)))) = ((5)+((4)+((3)+((3)))) =((5)+((4)+((6))) =((5)+(10)) =(15) this is the return value of sum(5) function How to represent Function call to have sum(n)? Function sum(n) If(n==1) return (1) Else return n+sum(n-1) Problem-2: Write a function called as fib(n) to calculate n-th member of a Fibonacci series. Use recursive call. Example: F1=1=fib(1) F2=1=fib(2) F3=F1+F2=1+1=2=fib(3) F1=f2=1 F2=f3=2 F3=f1+f2=1+2=3=fib(4) 1,1,2,3,5,8,13,21,34,55,89,… We want to implement the same using recursive call. Fib(n) name of the function If(n<=2) Return (1) Else Return (fib(n-1)+fib(n-2)) Suppose we want to calculate fib(5) Fib(5)fib(4)+fib(3)fib(2)+fib(1) Fib(4)fib(3)+fib(2) Fib(3)fib(2)+fib(1)
Problem_3: Write a program in R to implement
“Towers of Hanoi Algorithm”. D-1 D-1 D-2 D-2 D-1 D-3 D-1 D-3 D-2 Peg-1 Peg-1 Peg-2 Peg-3 The valid arrangements are as follows: D-1 D-1 D-2 D-2 D-3 D-3 Invalid arrangements are as follows: D-2 D-3 D-3 D-1 D-1 D-2 We have to move D-1, D-2 and D-3 from Peg-1 to Peg-2 using Peg-3. Manual Solution: Move Disk-1 from Peg-1 to Peg-2 Move Disk-2 from Peg-1 to Peg-3 Move Disk-1 from Peg-2 to Peg-3 Move Disk-3 from Peg-1 to Peg-2 Move Disk-1 from Peg-3 to Peg-1 Move Disk-2 from Peg-3 to Peg-2 Move Disk-1 from Peg-1 to Peg-2 The task is over. Number of movements taken=7=23 -1 For n-disks numbers of operations =2n-1 Algorithm to implement Towers of Hanoi problem: 1. Shift (n-1) disks from Peg-1 to Peg-3 using Peg-2 2. Shift n-th disk from Peg-1 to Peg-2 3. Now Peg-1 is empty but peg-3 contains (n-1) disks. Now shift (n-2) disks from Peg-3 to Peg-1. 4. Shift (n-1)-th disk from Peg-3 to Peg- 2.
The above steps we have to preat till peg-1
contains disk-1. 5. Finally move disk-1 from peg-1 to peg- 2.
The function may be written as follows:
hanoi(p1,p2,p3,nd) where Hanoi=name of the fi=unction, p1,p2 and p3 are the name of 3 different pegs, nd=total number of disks to be shifted/moved from peg-1 to peg-2 using peg-3. Q.No.16: Problem Sheet-1: Display all prime number <=n using a function is_prime(n) This function will return 1 if ‘n’ is prime otherwise it will return 0.