# recursive function

• 09-02-2010, 12:11 PM
jayden
recursive function
can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,

BEGIN program(num1, num2)
declare num3 as integer
WHILE num2 !=0
set num3 to the remainder of num1/num2
set num1 to num2
set num2 to num3
ENDWHILE
RETURN num1
END program
• 09-02-2010, 12:18 PM
JosAH
Quote:

Originally Posted by jayden
can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,

Yes I can and I'm sure others can do it too but can you do it? This is not a rent-a-coder forum; you show your attempts and tell us where you are stuck with it.

kind regards,

Jos
• 09-02-2010, 12:27 PM
jayden
i got as far as writing a recursive function that is extremly simple
BEGIN count(num)
IF num<3
print num
count (n+1)
END IF
END Count
but i cant seem to adapt the aspects of this algorithim into it any help/ pointers would be great
• 09-02-2010, 12:30 PM
jayden
it would be greatly appreciated if you could step me through the process of changing the algorithim to a recursive function. iam new to software development so please bare with me
• 09-02-2010, 01:38 PM
jayden
is their anyone who can help me out all i need is two or three lines of code and my problem is solved, please help me out
• 09-02-2010, 01:55 PM
JosAH
Quote:

Originally Posted by jayden
can anyone convert this functional paradigm algorithim to a recursive algorithim thanks,

BEGIN program(num1, num2)
declare num3 as integer
WHILE num2 !=0
set num3 to the remainder of num1/num2
set num1 to num2
set num2 to num3
ENDWHILE
RETURN num1
END program

You recursive method should have a structure like this:

Code:

```BEGIN program(num1, num2) IF (num2 == 0) RETURN num1 return program( ... , ...)```
Notice the recursive call at the end of the function body; you should pass the correct parameter values (those can be found/deduced from the original WHILE body).

kind regards,

Jos
• 09-02-2010, 02:02 PM
jayden
thank you very much appreciated
• 09-02-2010, 02:30 PM
jayden
BEGIN program (num1, num2)
declare num3 as int
IF(num2 == 0) RETURN num1
return program(num1/num2) to num3
(num1 to num2)
(num2 to num3)
ENDIF
Return num1
END program

hopefully this will work if not give me one more pointer in the right direction:)
cheers jayden
• 09-02-2010, 02:45 PM
JosAH
Quote:

Originally Posted by jayden
BEGIN program (num1, num2)
declare num3 as int
IF(num2 == 0) RETURN num1
return program(num1/num2) to num3
(num1 to num2)
(num2 to num3)
ENDIF
Return num1
END program

hopefully this will work if not give me one more pointer in the right direction:)
cheers jayden

We are talking all pseudo code but I'd rather say it'd be:

Code:

```BEGIN program(num1, num2) IF (num2 == 0) RETURN num1 return program( num2, remainder of num1 and num2) END program```
kind regards,

Jos
• 09-02-2010, 02:49 PM
jayden
what happened to num 3
• 09-02-2010, 02:54 PM
JosAH
Quote:

Originally Posted by jayden
what happened to num 3

You want it back? Here it is back again:

Code:

```BEGIN program(num1, num2) IF (num2 == 0) RETURN num1 DECLARE num3 as int num3= remainder of num1 and num2 return program(num2, num3) END program```
kind regards,

Jos
• 09-02-2010, 03:00 PM
jayden
that you again Jos your efforts are greatly appreciated
cheers Jayden