Import JJIO Class Recursions -- Name jmotil -- Does show recursive examples Function fact (num) ofType int is public Slot num ofType int Box result ofType int -- Does compute the factorial recursively If (num == 0) then Set result = 1 Else Set result = num * fact (num - 1) EndIf EndFunction fact Function sqr (n) ofType int is public Slot n ofType int Box result ofType int -- Does compute the square recursively If (n == 0) then Set result = 0 Else Set result = sqr (n-1) + n + n - 1 EndIf EndFunction sqr Function fib (n) ofType int is public Slot n ofType int Box result ofType int -- Does compute fibonacci recursively If (n == 0) then Set result = 1 ElseIf (n == 1) then Set result = 1 Else Set result = fib(n - 1) + fib(n-2) EndIf EndFunction fib Function power (x,n) ofType real is public Slot x ofType real Slot n ofType int Box result ofType real -- Does compute the power recursively If (n == 0) then Set result = 1.0 Else Set result = x * power (x, n-1) EndIf EndFunction power Function pow2 (x,n) ofType real is public Slot x ofType real Slot n ofType int Box result ofType real -- Does compute the power recursively If (n == 0) then Set result = 1.0 ElseIf ( (n % 2) == 0) then -- odd n Set result = x * pow2 (x, n-1) Else -- even n Set result = pow2 (x, n/2) * pow2 (x, n/2) EndIf EndFunction pow2 Routine doCurse (many, curse) is public Slot many ofType int Slot curse ofClass Str -- Does repeat curse many times If (many > 0) then Output curse Call doCurse with (many-1, curse) EndIf EndRoutine doCurse Routine mainTest (none) is private Boxes i,j,k ofType int -- Does test the recursive methods Start Set i = 5 Outputln fact (i) Outputln sqr (i) Outputln fib (i) Outputln power(2.0, i) Call doCurse with (i, "@#% ") EndRoutine mainTest EndClass Recursions