'// 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 ------------------------------ Output ------------------------------ n = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 F(n) = 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13 M(n) = 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12