106761000 Page 1 of 15 COS201 Fundamentals of PL – Lecture 6 AUBG, COS dept Lecture 6 Theory Iteration and Recursion in Procedure Oriented Programming. Basic notions. Syntax representation of iteration and recursion. Examples. Comparison of iterative and recursive algorithms. RS 6th ed., Section 8.3, Section 10.3.3 The essence of iteration and its syntax representation in the form of a variety of counter controlled and logically controlled loop statements. The essence of recursion and its syntax representation in the form of a subprogram call statement. Recursive function: A function that calls itself is said to be recursive. Recursion visualized: recursively-defined geometric figures, such as the Koch curve, the Sierpinski triangle, the Cantor set Recursion in Real Life, Recursion in Mathematics, Recursion in Computing Recursive algorithms: Simple case: Problem case for which a straightforward solution is known. The recursive algorithms generally consist of an if statement with the following form: if ( <this is a simple case> ) <solve it>; else <redefine the problem using recursion>; Conditional expression after McCarthy: Notation: b1, b2, … , bn Boolean expression(s) e1, e2, … , en, en+1 Expression(s) [ b1 > e1, b2 > e2, … , bn > en, en+1 ] Classification of recursion: primitive recursion Direct and indirect recursion. referenced recursion Time Performance and Memory Consumption of iterative and recursive versions of a certain set of algorithms: factorial, greatest common divisor, Fibonacci series: C, C++, Perl, Python source texts for recursive and iterative versions of algorithms for factorial fact(n) greatest common divisor gcd(m,n) sum of natural numbers 1 + 2 + 3 +… sum of the elements of 1dimensioned array to calculate a square root to calculate an element of Fibonacci series Ackermann function A(m, n) Hanoi towers game addition, multiplication, division Simulation of RTL itoa() function Problem: Factorial More on back page >> 106761000 Page 2 of 15 C/C++ Solution int facti(int n) { if (n==0 || n==1) return 1; Iteration Recursion C Driver Program C++ Driver Program int i, res=1; for (i=2; i<=n; i++) res *= i; return res; } int factr(int n) { if (n==0 || n==1) return 1; return n * factr(n-1); } #include <stdio.h> int facti(int n); int factr(int n); void main() { int i; printf("C Greeting Hello, World"); // Test Factoriel recursive and iterative versions for(i=0; i<=10; i++) { printf("\n%d -> %d -> %d", i, facti(i), factr(i)); #include <iostream.h> int facti(int n); int factr(int n); void main() { cout <<"CPP Greeting Hello, World"; for(int i=0;i<=10;i++) {cout<<"\n"<< i << " -> " << facti(i) << " -> " << factr(i); } } Perl Solution sub Facti { Iteration $Arg1 = shift(@_); if($Arg1 == 0) {return(1);} $Result = 1; for ($i=1; $i <= $Arg1; $i++) { $Result = $Result * $i; } return($Result); } my($Arg1) = shift(@_); if($Arg1 == 0) {return(1);} else {return($Arg1 * &Factr($Arg1-1)); } } Recursion sub Factr { Driver Program #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; for($k=0; $k<=10; $k++) {print "\n".$k." >> ".&Facti($k)." - ".&Factr($k);} Python Solution def facti(n): Iteration Recursion Driver Program i, res = 1, 1 while i <= n: res, i = res*i, i+1 return res def factr(n): if n==0: return 1 else: return n * factr(n-1) print "Python greeting Hello, world!“ listnum = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] print " " print "Factorial recursive and iterative versions" for k in listnum: print k, factr(k), facti(k) } } 106761000 Page 3 of 15 Problem: GCD /Greatest Common Divisor/ C/C++ Solution int gcdi(int m, int n) Iteration { if(n==0) return m; int r; while ( (r=m%n)!=0 ) { m=n; return n; Recursion C Driver Program C++ Driver Program } int gcdr(int m, int n) { if(n==0) return m; return gcdr(n, m%n); } #include <stdio.h> int gcdi(int m, int n); int gcdr(int m, int n); void main() { int i; printf("C Greeting Hello, World"); // Test GCD recursive and iterative versions for(i=0; i<=10; i++) { printf("\n10,%d -> %d -> %d", i, gcdi(10,i), gcdr(10,i)); } } #include <iostream.h> int gcdi(int m, int n); int gcdr(int m, int n); int main() { cout <<"CPP Greeting Hello, World"; // Test GCD recursive and iterative versions for(int i=0;i<=10;i++) { cout <<"\n10," << i << " -> " << gcdi(10,i) << " -> " << gcdr(10,i); } } Perl Solution sub GCDi { Iteration Recursion Driver Program n=r; } $Arg1 = shift(@_); $Arg2 = shift(@_); if($Arg2 == 0) {return($Arg1);} while( ($Rem=($Arg1%$Arg2)) != 0) { $Arg1 = $Arg2; $Arg2 = $Rem; } return($Arg2); } sub GCDr { my($Arg1) = shift(@_); my($Arg2) = shift(@_); if($Arg2 == 0) {return($Arg1);} else {return(&GCDr($Arg2, $Arg1%$Arg2)); } } #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nGCD"; for($l=0; $l<=10; $l++) {print "\n10,".$l." >> ".&GCDi(10, $l)." - ".&GCDr(10, $l);} Python Solution 106761000 Iteration Recursion Driver Program Page 4 of 15 def gcdi(m, n): if n==0: return m rem = m % n while rem != 0: m, n, rem = n, rem, m % n return n def gcdr(m, n): if n==0: return m else: return gcdr(n, m%n) print "Python greeting Hello, world!" listnum = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] print " " print "GCD recursive and iterative versions" for k in listnum: print 10, k, gcdr(10, k), gcdi(10, k) Problem: Sum of natural numbers Σi C/C++ Solution int sumi(int n) Iteration // Sum of natural numbers { if (n==0 || n==1) return n; int i, sum = 0; for (i=1; i<=n; i++) sum+= i; return sum; Recursion C Driver Program C++ Driver Program } int sumr(int n) { if (n==0 || n==1) return n; return n + sumr(n-1); } #include <stdio.h> int sumi(int n); int sumr(int n); void main() { int i; printf("C Greeting Hello, World"); // Test Sum of natural numbers recursive and iterative versions printf("\n\nSum of natural numbers"); for(i=0; i<=10; i++) { printf("\n%d -> %d -> %d", i, sumi(i), sumr(i)); } } #include <iostream.h> int sumi(int n); int sumr(int n); int main() { cout <<"CPP Greeting Hello, World"; // Test Sum of natural numbers recursive and iterative versions cout <<"\n\nSum of natural numbers"; for(int i=0;i<=10;i++) { cout <<"\n" << i << " -> " << sumi(i) << " -> " << sumr(i); } } Perl Solution 106761000 Page 5 of 15 Iteration sub sumi { $Arg1 = shift(@_); if($Arg1==0 || $Arg1==1) {return($Arg1);} $Sum = 0; for ($i=1; $i <= $Arg1 ; $i++) {$Sum += $i;} return($Sum); } my($Arg1) = shift(@_); if($Arg1==0 || $Arg1==1) {return($Arg1);} return($Arg1 + &sumr($Arg1-1)); } Recursion sub sumr { Driver Program #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nSum of natural numbers"; for($l=0; $l<=10; $l++) {print "\n".$l." >> ".&sumi($l)." - ".&sumr($l);} Python Solution # Sum of natural numbers Iteration Recursion Driver Program def sumi(n): if n==0: return n i, sum = 1, 0 while i <= n: sum, i = sum + i, i+1 return sum def sumr(n): if n==0: return n else: return n + sumr(n-1) print "Python greeting Hello, world!" listnum = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] print " " print "Sum natural numbers recursive and iterative versions" for k in listnum: print k, sumr(k), sumi(k) Problem: Sum of 1-dimensioned array elements Σa[I] C/C++ Solution // Sum of 1-dimensioned array elements Iteration Recursion int ar1sumi(int b[], int n) { int i, sum=0; for (i=0; i<=n; i++) sum += b[i]; return sum; } int ar1sumr(int b[], int n) { if (n==0) return b[0]; return b[n] + ar1sumr(b, n-1); } #include <stdio.h> 106761000 C Driver Program C++ Driver Program Page 6 of 15 // Sum of 1-dmensioned array elements int ar1sumi(int b[], int n); int ar1sumr(int b[], int n); void main() { int i; printf("C Greeting Hello, World"); // Test Sum of 1-dimensioned array elements printf("\n\nSum of 1-dimenisioned array elements\n"); int mas1[]={ 5, 10, 20, 30, 40, 50, 60}; for (i=0; i<=6; i++) printf("%d ", mas1[i]); printf(" >> %d >> %d", ar1sumi(mas1, 6), ar1sumr(mas1, 6)); } #include <iostream.h> // Sum of 1-dmensioned array elements int ar1sumi(int b[], int n); int ar1sumr(int b[], int n); int main() { cout <<"CPP Greeting Hello, World"; // Test Sum of 1-dimensioned array elements cout <<"\n\nSum of 1-dimenisioned array elements\n"; int mas1[] = { 5, 10, 20, 30, 40, 50, 60}; for (int i=0; i<=6; i++) cout << mas1[i] << " "; cout << " > " << ar1sumi(mas1, 6) << " > " << ar1sumr(mas1, 6); } Perl Solution sub ar1sumi{ @ArgList = @_; $sum = 0; Iteration Recursion Driver Program for ($i=0;$i<=$#ArgList;$i++) {$sum+=$ArgList[$i];} return($sum); } sub ar1sumr{ my(@ArgList) = @_; my($Index)=$#ArgList; if ($Index==0) {return ($ArgList[$Index]);} return ($ArgList[0]+ar1sumr(@ArgList[1..$Index])); } #!/usr/bin/perl print "\nSum of 1-dimensioned array elements "; @mas = ( 5, 10, 20, 30, 40, 50, 60 ); for ($i=0; $i<=$#mas; $i++) { print $mas[$i]." ";} print "\nIterative sum of an array/list = ".ar1sumi(@mas); print "\nRecursive sum of an array/list = ".ar1sumr(@mas); getc(); Python Solution Iteration Recursion Driver Program Problem: Fibonacci series 106761000 Page 7 of 15 C/C++ Solution int fibi(int n) { Iteration Recursion C Driver Program C++ Driver Program } int fibr(int n) { int i, va=1, vb=1, vc=1; if (n==0 || n==1) return 1; for (i=2; i<=n; i++) { vc = va + vb; va = vb; vb = vc; } return vc; if (n==0 || n==1) return 1; return fibr(n-1) + fibr(n-2); } #include <stdio.h> int fibi(int n); int fibr(int n); void main() { int i; printf("C Greeting Hello, World"); // Test Fibonacci recursive and iterative versions printf("\n\nFibonacci"); for(i=0; i<=10; i++) {printf("\n%d -> %d -> %d", i, fibi(i), fibr(i)); } } #include <iostream.h> int fibi(int n); int fibr(int n); int main() { cout <<"CPP Greeting Hello, World"; // Test Fibonacci recursive and iterative versions cout <<"\n\nFibonacci"; for(int i=0;i<=10;i++) {cout <<"\n" << i << " -> " << fibi(i) << " -> " << fibr(i); } Perl Solution sub fibi { Iteration $Arg1 = shift(@_); if($Arg1==0 || $Arg1==1) {return($Arg1);} $va=0; $vb=1; for ($i=2; $i <= $Arg1 ; $i++) {$vc = $va+$vb; $va = $vb; $vb = $vc;} return($vc); } my($Arg1) = shift(@_); if($Arg1==0 || $Arg1==1) {return($Arg1);} return( &fibr($Arg1-1) + &fibr($Arg1-2) ); } Recursion sub fibr { Driver Program #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nFibonacci"; for($l=0; $l<=10; $l++) {print "\n".$l." >> ".&fibi($l)." - ".&fibr($l);} getc(); Python Solution } 106761000 Iteration Recursion Driver Program Page 8 of 15 # Fibonacci series module def fibi(n): # Fibonacci series up to n va, vb, i = 0, 1, 2 while i <= n: va, vb, i = vb, va+vb, i+1 return vb def fibr(n): if n==0: return n elif n==1: return n else: return fibr(n-1) + fibr(n-2) print "Python greeting Hello, world!" listnum = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] print "Fibonacci recursive and iterative versions" for k in listnum: print k, fibr(k), fibi(k) #read (ch) Problem: Square Root C/C++ Solution // Square Root simulations Iteration double sqrti(double x, double eps) { Recursion C Driver Program C++ Driver Program double yn=1, yn1=(x/yn+yn)/2; while (abs(yn-yn1)>eps) {yn=yn1; yn1=(x/yn+yn)/2;} return yn; } double sqrtr(double x, double start, double eps) { if (abs(start*start-x) < eps) return start; return sqrtr(x, (start*start +x)/(2*start), eps); } #include <stdio.h> double sqrti(double x, double eps); double sqrtr(double x, double start, double eps); void main() { printf("C Greeting Hello, World"); // Test Square Root recursive and iterative versions printf("\n\nSquare root simulations"); printf("\n 25 -> %lf -> %lf", sqrti( 25,0.001), sqrtr( 25,1,0.001)); printf("\n144 -> %lf -> %lf", sqrti(144,0.001), sqrtr(144,1,0.001)); printf("\n289 -> %lf -> %lf", sqrti(289,0.001), sqrtr(289,1,0.001)); } #include <iostream.h> double sqrti(double x, double eps); double sqrtr(double x, double start, double eps); int main() { cout <<"CPP Greeting Hello, World"; // Test Square Root recursive and iterative versions cout <<"\n\nSquare root simulations"; cout<<"\n"<< 25<<" -> "<<sqrti(25,0.001)<<" -> "<< sqrtr(25,1,0.001); cout<<"\n"<<144<<" -> "<<sqrti(144,0.001)<<" -> "<< sqrtr(144,1,0.001); cout <<"\n"<<289<<" -> "<<sqrti(289,0.001)<<" -> "<< sqrtr(289,1,0.001); } Perl Solution 106761000 Iteration Recursion Driver Program Page 9 of 15 sub sqrti { $Arg1 = shift(@_); $Arg2 = shift(@_); $yn=1.; $yn1=($Arg1/$yn + $yn)/2.; while( abs($yn-$yn1) > $Arg2) { $yn =$yn1; $yn1=($Arg1/$yn + $yn)/2. } return($yn); } sub sqrtr { my($Arg1) = shift(@_); my($Arg2) = shift(@_); my($Arg3) = shift(@_); if(abs($Arg2*$Arg2-$Arg1)<$Arg3) {return($Arg2);} return(&sqrtr($Arg1,($Arg2*$Arg2+$Arg1)/(2.*$Arg2),$Arg3)); } #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nSquare root simulations"; print "\n 25 >> ".&sqrti( 25,0.001)." - ".&sqrtr( 25,1.,0.001); print "\n144 >> ".&sqrti(144,0.001)." - ".&sqrtr(144,1.,0.001); print "\n289 >> ".&sqrti(289,0.001)." - ".&sqrtr(289,1.,0.001); getc(); Python Solution # square root simulations Iteration Recursion Driver Program def sqrti(x, eps): yn = 1 yn1 = (x/yn + yn)/2 while abs(yn-yn1)>eps: yn = yn1 yn1 = (x/yn+yn)/2 return yn def sqrtr(x, yn, eps): if abs(yn*yn-x)<eps: return yn else: return sqrtr(x, (yn*yn + x)/(2*yn), eps) print " " print "SQRT recursive and iterative versions" listx = [ 25, 49, 81, 100, 289 ] for k in listx: print k, sqrtr(k, 1, 0.001), sqrti(k, 0.001) Problem: Ackerman Function C/C++ Solution Iteration // Ackerman function Recursion int acker(int m, int n) { if(m==0) return n+1; if(n==0) return acker(m-1,1); return acker(m-1, acker(m,n-1)); } C Driver #include <stdio.h> int acker(int m, int n); 106761000 Program C++ Driver Program Page 10 of 15 void main() { printf("C Greeting Hello, World"); // Test Ackerman recursive version printf("\n\nAckerman function acker(m,n)"); printf("\nAckerman Arguments 0,0 > %d", acker(0,0)); printf("\nAckerman Arguments 1,1 > %d", acker(1,1)); printf("\nAckerman Arguments 2,1 > %d", acker(2,1)); printf("\nAckerman Arguments 0,51 > %d", acker(0,51)); } #include <iostream.h> int acker(int m, int n); int main() { cout <<"CPP Greeting Hello, World"; // Test Ackerman recursive version cout <<"\n\nAckerman function acker(m,n)"; cout <<"\nAckerman Arguments 0,0 > " << acker(0,0); cout <<"\nAckerman Arguments 1 1 > " << acker(1,1); cout <<"\nAckerman Arguments 2 1 > " << acker(2,1); cout <<"\nAckerman Arguments 0 51 > " << acker(0,51); } Perl Solution Iteration sub acker { Recursion Driver Program my($Arg1) = shift(@_); my($Arg2) = shift(@_); if($Arg1==0) {return($Arg2 + 1);} if($Arg2==0) {return(&acker($Arg1-1, 1));} return(&acker($Arg1-1, &acker($Arg1, $Arg2-1))); } #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nAckerman function acker(m,n)"; print "\nAckerman Arguments 0,0 > ".&acker(0,0); print "\nAckerman Arguments 1,1 > ".&acker(1,1); print "\nAckerman Arguments 2,1 > ".&acker(2,1); print "\nAckerman Arguments 0,51 > ".&acker(0,51); getc(); Python Solution Iteration # Ackerman function Recursion Driver Program def acker(m, n): if m==0: return n+1 if n==0: return acker(m-1, 1) else: return acker(m-1, acker(m, n-1)) print "Python greeting Hello, world!" print " " print "Ackerman function recursive version" print acker(0, 0), acker(1, 1), acker(2, 1), acker(0,51) Problem: Hanoi towers 106761000 Page 11 of 15 C/C++ Solution Iteration // Hanoi Towers game Recursion C Driver Program C++ Driver Program void trantower(int ndsks, int fromndl, int tondl, int wrkndl) { if (ndsks > 0) { trantower(ndsks-1, fromndl, wrkndl, tondl); printf("\n%2d >> %2d", fromndl, tondl); trantower(ndsks-1, wrkndl, tondl, fromndl); } } #include <stdio.h> void trantower(int ndsks, int fromndl, int tondl, int wrkndl); void main() { printf("C Greeting Hello, World"); // Test Hanoi Towers game recursive version printf("\n\nHanoi towers game - 1 disk"); trantower(1, 1, 3, 2); printf("\n\nHanoi towers game - 2 disks"); trantower(2, 1, 3, 2); printf("\n\nHanoi towers game - 3 disks"); trantower(3, 1, 3, 2); } #include <iostream.h> void trantower(int ndsks, int fromndl, int tondl, int wrkndl); int main() { cout <<"CPP Greeting Hello, World"; // Test Hanoi Towers game recursive version cout <<"\n\nHanoi towers game - 1 disk"; trantower(1, 1, 3, 2); cout <<"\n\nHanoi towers game - 2 disks"; trantower(2, 1, 3, 2); cout <<"\n\nHanoi towers game - 3 disks"; trantower(3, 1, 3, 2); } Perl Solution Iteration sub trantower{ my($Arg1) = shift(@_); my($Arg2) = shift(@_); Recursion my($Arg3) = shift(@_); my($Arg4) = shift(@_); if($Arg1 >= 1) { trantower($Arg1-1,$Arg2,$Arg4,$Arg3); print "\n".$Arg2." >> ".$Arg3; trantower($Arg1-1,$Arg4,$Arg3,$Arg2); } } Driver Program #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\n\nHanoi towers game - 1 disk "; &trantower(1, 1, 3, 2); print "\n\nHanoi towers game - 2 disks"; &trantower(2, 1, 3, 2); print "\n\nHanoi towers game - 3 disks"; &trantower(3, 1, 3, 2); getc(); Python Solution 106761000 Iteration Recursion Driver Program Page 12 of 15 # Hanoi towers game def trantower(ndsks, fromndl, tondl, wrkndl): if ndsks>=1: trantower(ndsks-1, fromndl, wrkndl, tondl) print fromndl, " >> ", tondl trantower(ndsks-1, wrkndl, tondl, fromndl) print "Python greeting Hello, world!" print " " print "Hanoi towers game - recursive function implemented" trantower(1, 1, 3, 2) print" " trantower(2, 1, 3, 2) print" " trantower(3, 1, 3, 2) Problem: RTL itoa() simulated C/C++ Solution Iteration // C/C++ itoa run time function simulated Recursion void printd(int n) { if (n<0) { putchar('-'); n = -n;} if(n/10 != 0) printd(n/10); putchar(n%10+'0'); C Driver Program C++ Driver Program } #include <stdio.h> void printd(int n); void main() { printf("C Greeting Hello, World"); // Test C/C++ itoa run time function recursive version printf("\n\nC/C++ itoa run time library function"); printf("\n"); printd(123); printf("\n"); printd(-86); } // C/C++ itoa run time function simulated void printd(int n) { if (n<0) { cout << "-"; n = -n;} if(n/10 != 0) printd(n/10); cout << n%10; } //-----------------------------------------------------------------------#include <iostream.h> void printd(int n); int main() { cout <<"CPP Greeting Hello, World"; // Test C/C++ itoa run time function recursive version cout <<"\n\nC/C++ itoa run time library function"; cout << "\n"; printd(123); cout << "\n"; printd(-86); } Perl Solution 106761000 Iteration Recursion Driver Program Page 13 of 15 sub printd { my($Arg1) = shift(@_); if($Arg1<0){ print"-"; $Arg1 = -$Arg1; } if( ($Arg1/10) != 0) { &printd($Arg1/10); } if ($Arg1%10 != 0) {print chr($Arg1%10 + 48);} } #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; print "\nC/C++ itoa run time library function"; print "\n"; &printd(123); print "\n"; &printd(-86); print "\n"; &printd(5); getc(); Python Solution Iteration # C/C++ run-time function itoa recursively simulated Recursion Driver Program def printd(n): if n<0: print"-", if n<0: n = -n if n/10!=0: printd(n/10) print n%10, print "Python greeting Hello, world!" print " " print "The itoa run-time function recursively implemented as printd" printd(123) print " " printd(-567890) print " " printd(86) Problem: multiplication (*) operator C/C++ Solution int muli(int m, int n) Iteration { if (n==0) return 0; if (n==1) return m; int i, res=0; for (i=1; i<=n; i++) { res = res + m;} return res; Recursion } int mulr(int m, int n) { if (n==0) return 0; if (n==1) return m; return m + mulr(m, n-1); } 106761000 C Driver Program C++ Driver Program Page 14 of 15 #include <stdio.h> int muli(int m, int n); int mulr(int m, int n); void main() { int k, l; printf("C Greeting Hello, World"); // Test multiplication mplemented as recursive and iterative rourtine printf("\n\nMultiplication implemented as Recursion and Iteration"); for (k=0; k<=12; k++) { printf("\n"); for (l=0; l<=12; l++) printf("\n%d,%d -> %d -> %d", k, l, muli(k,l), mulr(k,l)); } } #include <iostream.h> int muli(int m, int n); int mulr(int m, int n); void main() { cout <<"CPP Greeting Hello, World"; // Test multiplication mplemented as recursive and iterative rourtine cout <<"\n\nMultiplication implemented as Recursion and Iteration"; for (int k=0; k<=12; k++) { cout <<"\n"; for (int l=0; l<=12; l++) cout <<"\n"<<k<<","<<l<<" -> "<<muli(k,l)<<" -> "<<mulr(k,l);} } Perl Solution sub muli { Iteration $Arg1 = shift(@_); $Arg2 = shift(@_); if($Arg2==0) { return 0;} if($Arg2==1) { return $Arg1;} $res = 0; for ($i=1; $i<=$Arg2; $i++) { $res = $res + $Arg1; } return $res; } $Arg1 = shift(@_); $Arg2 = shift(@_); if($Arg2==0) { return 0;} if($Arg2==1) { return $Arg1;} return $Arg1 + &mulr($Arg1, $Arg2-1); } Recursion sub mulr { Driver Program #!/usr/bin/perl # start main program untitled unstructured print "\nPerl greeting: Hello, world!\n"; # Test for MULTIPLICATION based on Recursion and Iteration print "\nMultiplication implemented using Recursion and Iteration"; for($k=0; $k<=12; $k++) { print "\n"; for($l=0; $l<=12; $l++) { print "\n".$k." ".$l." ".&muli($l, $k)." ".&mulr($l, $k); } } getc(); Python Solution 106761000 Iteration Recursion Driver Program Page 15 of 15 # Recursive and iterative simulation of MULTIPLICATION def muli(m, n): if n==0: return 0 if n==1: return m res, i = 0, 1 while i<=n: res, i = res+m, i+1 return res def mulr(m, n): if n==0: return 0 if n==1: return m return m + mulr(m,n-1) print "Python greeting Hello, world!" listnum = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] print " " print "Multiplication impl. using Recursion and Iteration" for k in listnum: print " " for l in listx: print k, l, mulr(k, l), muli(k, l)