Mutual recursion: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,037: | Line 1,037: | ||
M sequence. |
M sequence. |
||
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12</pre> |
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12</pre> |
||
=={{header|LibrOffice Basic}}== |
|||
<lang LibreOffice Basic>'// LibreOffice Basic Implementation of Hofstadter Female-Male sequences |
|||
'// Utility functions |
|||
sub setfont(strfont) |
|||
ThisComponent.getCurrentController.getViewCursor.charFontName = strfont |
|||
end sub |
|||
sub newline |
|||
oVC = thisComponent.getCurrentController.getViewCursor |
|||
oText = oVC.text |
|||
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) |
|||
end sub |
|||
sub out(sString) |
|||
oVC = ThisComponent.getCurrentController.getViewCursor |
|||
oText = oVC.text |
|||
oText.insertString(oVC, sString, false) |
|||
end sub |
|||
sub outln(optional sString) |
|||
if not ismissing (sString) then out(sString) |
|||
newline |
|||
end sub |
|||
function intformat(n as integer,nlen as integer) as string |
|||
dim nstr as string |
|||
nstr = CStr(n) |
|||
while len(nstr) < nlen |
|||
nstr = " " & nstr |
|||
wend |
|||
intformat = nstr |
|||
end function |
|||
'// Hofstadter Female-Male function definitions |
|||
function F(n as long) as long |
|||
if n = 0 Then |
|||
F = 1 |
|||
elseif n > 0 Then |
|||
F = n - M(F(n - 1)) |
|||
endif |
|||
end function |
|||
function M(n) |
|||
if n = 0 Then |
|||
M = 0 |
|||
elseif n > 0 Then |
|||
M = n - F(M(n - 1)) |
|||
endif |
|||
end function |
|||
'// Hofstadter Female Male sequence demo routine |
|||
sub Hofstadter_Female_Male_Demo |
|||
'// Introductory Text |
|||
setfont("LM Roman 10") |
|||
outln("Rosetta Code Hofstadter Female and Male Sequence Challenge") |
|||
outln |
|||
out("Two functions are said to be mutually recursive if the first calls the second,") |
|||
outln(" and in turn the second calls the first.") |
|||
out("Write two mutually recursive functions that compute members of the Hofstadter") |
|||
outln(" Female and Male sequences defined as:") |
|||
outln |
|||
setfont("LM Mono Slanted 10") |
|||
outln(chr(9)+"F(0) = 1 ; M(0)=0") |
|||
outln(chr(9)+"F(n) = n - M(F(n-1)), n > 0") |
|||
outln(chr(9)+"M(n) = n - F(M(n-1)), n > 0") |
|||
outln |
|||
'// Sequence Generation |
|||
const nmax as long = 20 |
|||
dim n as long |
|||
setfont("LM Mono 10") |
|||
out("n = " |
|||
for n = 0 to nmax |
|||
out(" " + intformat(n, 2)) |
|||
next n |
|||
outln |
|||
out("F(n) = " |
|||
for n = 0 to nmax |
|||
out(" " + intformat(F(n),2)) |
|||
next n |
|||
outln |
|||
out("M(n) = " |
|||
for n = 0 to nmax |
|||
out(" " + intformat(M(n), 2)) |
|||
next n |
|||
outln |
|||
end sub |
|||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |