Sudan function: Difference between revisions

added RPL
(Corrected syntax highlight tags)
(added RPL)
 
(16 intermediate revisions by 10 users not shown)
Line 202:
2 (2 sudan) 1
27</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">sudan: function [n, x, y][
if n = 0 -> return x + y
if y = 0 -> return x
 
sudan n-1 sudan n x y-1 y + sudan n x y-1
]
 
print sudan 1 3 3</syntaxhighlight>
 
{{out}}
 
<pre>35</pre>
 
=={{header|AWK}}==
Line 295 ⟶ 309:
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Function F(n As Integer, x As Integer, y As Integer) As Integer
If n = 0 Then Return x + y
If y = 0 Then Return x
Return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
End Function
 
Public Sub Main()
Dim n, x, y As Integer
 
For n = 0 To 1
Print " Values of F(" & n & ", x, y):"
Print " y/x 0 1 2 3 4 5"
Print String(29, "-") 'Print " ----------------------------"
For y = 0 To 6
Print y; " | ";
For x = 0 To 5
Print Format$(F(n, x, y), "####");
Next
Print
Next
Print
Next
Print "F(2,1,1) = "; F(2, 1, 1)
Print "F(3,1,1) = "; F(3, 1, 1)
Print "F(2,2,1) = "; F(2, 2, 1)
End </syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 331 ⟶ 381:
{{out}}
<pre>Similat to FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">FUNCTION F(n,x,y)
IF n = 0 THEN
LET F = x + y
ELSE
IF y = 0 THEN
LET F = x
ELSE
LET F = F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
END IF
END IF
END FUNCTION
 
PRINT "F(2,1,1) = "; F(2, 1, 1)
PRINT "F(3,1,1) = "; F(3, 1, 1)
PRINT "F(2,2,1) = "; F(2, 2, 1)
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|QBasic}}
{{works with|True BASIC}}
<syntaxhighlight lang="vb">FUNCTION F(n,x,y)
IF n = 0 THEN
LET F = x + y
ELSE
IF y = 0 THEN
LET F = x
ELSE
LET F = F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
END IF
END IF
END FUNCTION
 
PRINT "F(2,1,1) = "; F(2, 1, 1)
PRINT "F(3,1,1) = "; F(3, 1, 1)
PRINT "F(2,2,1) = "; F(2, 2, 1)
END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|QBasic}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">FUNCTION F(n,x,y)
IF n = 0 THEN
LET F = x + y
ELSE
IF y = 0 THEN
LET F = x
ELSE
LET F = F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
END IF
END IF
END FUNCTION
 
PRINT "F(2,1,1) = "; F(2, 1, 1)
PRINT "F(3,1,1) = "; F(3, 1, 1)
PRINT "F(2,2,1) = "; F(2, 2, 1)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Sudan function"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION F (n, x, y)
 
FUNCTION Entry ()
FOR n = 0 TO 1
PRINT " Values of F("; n; ", x, y ):"
PRINT " y/x 0 1 2 3 4 5"
PRINT " ----------------------------"
FOR y = 0 TO 6
PRINT y; " |";
FOR x = 0 TO 5
PRINT FORMAT$("####", F(n, x, y));
NEXT x
PRINT
NEXT y
PRINT
NEXT n
 
PRINT "F(2,1,1) ="; F(2, 1, 1)
PRINT "F(3,1,1) ="; F(3, 1, 1)
PRINT "F(2,2,1) ="; F(2, 2, 1)
 
END FUNCTION
 
FUNCTION F (n, x, y)
IF n = 0 THEN
RETURN x + y
ELSE
IF y = 0 THEN
RETURN x
ELSE
RETURN F (n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
ENDIF
ENDIF
 
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
Line 616 ⟶ 779:
sudan(2, 2, 1) = 27
sudan(3, 1, 1) = 10228</pre>
 
=={{header|Dart}}==
{{trans|C++}}
<syntaxhighlight lang="dart">int F(int n, int x, int y) {
if (n == 0) {
return x + y;
} else if (y == 0) {
return x;
}
 
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}
 
void main() {
print('F(1,3,3) = ${F(1, 3, 3)}');
}</syntaxhighlight>
{{out}}
<pre>Same as C++ entry.</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
function SudanFunction(N,X,Y: integer): integer;
begin
if n = 0 then Result:=X + Y
else if y = 0 then Result:=X
else Result:=SudanFunction(N - 1, SudanFunction(N, X, Y - 1), SudanFunction(N, X, Y - 1) + Y);
end;
 
 
procedure ShowSudanFunction(Memo: TMemo; N,X,Y: integer);
begin
Memo.Lines.Add(Format('Sudan(%d,%d,%d)=%d',[n,x,y,SudanFunction(N,X,Y)]));
end;
 
 
procedure ShowSudanFunctions(Memo: TMemo);
var N,X,Y: integer;
var S: string;
begin
for N:=0 to 1 do
begin
Memo.Lines.Add(Format('Sudan(%d,X,Y)',[N]));
Memo.Lines.Add('Y/X 0 1 2 3 4 5');
Memo.Lines.Add('----------------------------');
for Y:=0 to 6 do
begin
S:=Format('%2d | ',[Y]);
for X:=0 to 5 do
begin
S:=S+Format('%3d ',[SudanFunction(N,X,Y)]);
end;
Memo.Lines.Add(S);
end;
Memo.Lines.Add('');
end;
 
ShowSudanFunction(Memo, 1, 3, 3);
ShowSudanFunction(Memo, 2, 1, 1);
ShowSudanFunction(Memo, 2, 2, 1);
ShowSudanFunction(Memo, 3, 1, 1);
end;
 
 
 
 
</syntaxhighlight>
{{out}}
<pre>
Sudan(0,X,Y)
Y/X 0 1 2 3 4 5
----------------------------
0 | 0 1 2 3 4 5
1 | 1 2 3 4 5 6
2 | 2 3 4 5 6 7
3 | 3 4 5 6 7 8
4 | 4 5 6 7 8 9
5 | 5 6 7 8 9 10
6 | 6 7 8 9 10 11
 
Sudan(1,X,Y)
Y/X 0 1 2 3 4 5
----------------------------
0 | 0 1 2 3 4 5
1 | 1 3 5 7 9 11
2 | 4 8 12 16 20 24
3 | 11 19 27 35 43 51
4 | 26 42 58 74 90 106
5 | 57 89 121 153 185 217
6 | 120 184 248 312 376 440
 
Sudan(1,3,3)=35
Sudan(2,1,1)=8
Sudan(2,2,1)=27
Sudan(3,1,1)=10228
 
Elapsed Time: 47.644 ms.
 
</pre>
 
 
=={{header|Draco}}==
Line 679 ⟶ 947:
sudan(2, 2, 1) = 27
sudan(3, 1, 1) = 10228</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func f n x y .
if n = 0
return x + y
.
if y = 0
return x
.
return f (n - 1) f n x (y - 1) (f n x (y - 1) + y)
.
print "F(1,3,3) = " & f 1 3 3
</syntaxhighlight>
 
{{out}}
<pre>
F(1,3,3) = 35
</pre>
 
=={{header|F_Sharp|F#}}==
Line 991 ⟶ 1,279:
sudan(2, 2, 1) = 27
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function F (n, x, y)
if n == 0 then
return x + y
elseif y == 0 then
return x
else
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
end
end
 
local testCases = {
{0, 0, 0},
{1, 1, 1},
{1, 3, 3},
{2, 1, 1},
{2, 2, 1},
{3, 1, 1}
}
 
for _, v in pairs(testCases) do
io.write("F(" .. table.concat(v, ",") .. ") = ")
print(F(unpack(v)))
end</syntaxhighlight>
{{out}}
<pre>F(0,0,0) = 0
F(1,1,1) = 3
F(1,3,3) = 35
F(2,1,1) = 8
F(2,2,1) = 27
F(3,1,1) = 10228</pre>
 
=={{header|MAD}}==
Line 1,079 ⟶ 1,399:
sudan(3, 1, 1) = 10228
sudan(2, 2, 1) = 27</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[unicode, strformat]
 
func sudan(n, x, y: Natural): int =
if n == 0: return x + y
if y == 0: return x
let z = sudan(n, x, y - 1)
return sudan(n - 1, z, z + y)
 
const Delta = ord("₀".toRunes()[0]) - ord('0')
 
func subscript(n: Natural): string =
for c in $n:
result.add Rune(ord(c) + Delta)
 
for (n, x, y) in [(0, 0, 0), (1, 1, 1), (2, 1, 1), (2, 2, 1), (2, 2, 2), (3, 1, 1)]:
echo &"F{subscript(n)}({x}, {y}) = {sudan(n, x, y)}"
</syntaxhighlight>
 
{{out}}
<pre>F₀(0, 0) = 0
F₁(1, 1) = 3
F₂(1, 1) = 8
F₂(2, 1) = 27
F₂(2, 2) = 15569256417
F₃(1, 1) = 10228
</pre>
 
=={{header|OCaml}}==
Line 1,093 ⟶ 1,441:
- : int = 15569256417
</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">
/* REXX implement the SUDAN function */
Say '+---++-------------------------+'
Say '| y ||x= 0 1 2 3 4 5 |'
Say '+===++=========================+'
Do y=0 To 6
s='|' y '||'
Do x=0 To 5
s=s format(sudan(x,y),3)
End
Say s '|'
End
Say '+===++=========================+'
Exit
 
sudan: Procedure
Parse Arg x,y
Return sudan1(1,x,y)
 
sudan1: Procedure
Parse Arg n,x,y
Select
When n=0 Then Return x+y
When y=0 Then Return x
Otherwise Return sudan1(n-1,sudan1(n,x,y-1),sudan1(n,x,y-1)+y)
End</syntaxhighlight>
{{out|output}}
<pre>+---++-------------------------+
| y ||x= 0 1 2 3 4 5 |
+===++=========================+
| 0 || 0 1 2 3 4 5 |
| 1 || 1 3 5 7 9 11 |
| 2 || 4 8 12 16 20 24 |
| 3 || 11 19 27 35 43 51 |
| 4 || 26 42 58 74 90 106 |
| 5 || 57 89 121 153 185 217 |
| 6 || 120 184 248 312 376 440 |
+===++=========================+</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
{{trans|Delphi}}
<syntaxhighlight lang="pascal">
program Sudan;
//trans https://rosettacode.org/wiki/Sudan_function#Delphi
{$IFDEF FPC} {$MODE DELPHI} {$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
function SudanFunction(N,X,Y: UInt64): UInt64;
begin
if n = 0 then
Result:=X + Y
else
if y = 0 then
Result:=X
else
Result:=SudanFunction(N - 1, SudanFunction(N, X, Y - 1), SudanFunction(N, X, Y - 1) + Y);
end;
 
procedure ShowSudanFunction(N,X,Y: UInt64);
begin
writeln(Format('Sudan(%d,%d,%d)=%d',[n,x,y,SudanFunction(N,X,Y)]));
end;
 
procedure ShowSudanFunctions;
var
N,X,Y: UInt64;
S: string;
begin
for N:=0 to 1 do
begin
writeln(Format('Sudan(%d,X,Y)',[N]));
writeln('Y/X 0 1 2 3 4 5');
writeln('----------------------------');
for Y:=0 to 6 do
begin
S:=Format('%2d | ',[Y]);
for X:=0 to 5 do
begin
S:=S+Format('%3d ',[SudanFunction(N,X,Y)]);
end;
writeln(S);
end;
writeln('');
end;
end;
 
BEGIN
ShowSudanFunctions;
ShowSudanFunction( 1, 3, 3);
ShowSudanFunction( 2, 1, 1);
ShowSudanFunction( 2, 2, 1);
ShowSudanFunction( 2, 1, 2);
ShowSudanFunction( 3, 1, 1);
ShowSudanFunction( 2, 2, 2);
END.</syntaxhighlight>
{{out|@TIO.RUN}}<pre>
Sudan(0,X,Y)
Y/X 0 1 2 3 4 5
----------------------------
0 | 0 1 2 3 4 5
1 | 1 2 3 4 5 6
2 | 2 3 4 5 6 7
3 | 3 4 5 6 7 8
4 | 4 5 6 7 8 9
5 | 5 6 7 8 9 10
6 | 6 7 8 9 10 11
 
Sudan(1,X,Y)
Y/X 0 1 2 3 4 5
----------------------------
0 | 0 1 2 3 4 5
1 | 1 3 5 7 9 11
2 | 4 8 12 16 20 24
3 | 11 19 27 35 43 51
4 | 26 42 58 74 90 106
5 | 57 89 121 153 185 217
6 | 120 184 248 312 376 440
 
Sudan(1,3,3)=35
Sudan(2,1,1)=8
Sudan(2,2,1)=27
Sudan(2,1,2)=10228
Sudan(3,1,1)=10228
Sudan(2,2,2)=15569256417
 
Real time: 3.822 s CPU share: 98.74 %</pre>
 
=={{header|Perl}}==
Line 1,319 ⟶ 1,796:
|14||14||29||60||123||250||505||1016||2039||4086||8181||16372||32755||65522||131057||262128
|}
 
=={{header|RPL}}==
{{works with|RPL|HP|48-R}}
« '''IF''' DUP2 2 GET AND '''THEN'''
DUP 2 GET 3 PICK ROT { 0 1 } - <span style="color:blue">SUDAN</span>
SWAP OVER + 2 →LIST SWAP 1 - SWAP <span style="color:blue">SUDAN</span>
'''ELSE''' ∑LIST SWAP DROP '''END'''
» '<span style="color:blue">SUDAN</span>' STO <span style="color:grey">''@ ( n { x y } ) → F<sub>n</sub>(x,y) )''</span>
 
{ { 0 { 0 0 } } { 1 { 1 1 } } { 1 { 3 3 } } { 2 { 1 1 } } { 2 { 2 1 } } { 3 { 1 1 } } } 1 « EVAL <span style="color:blue">SUDAN</span> » DOLIST
 
{{out}}
<pre>
1: { 0 3 35 8 27 10228 }
</pre>
 
=={{header|Ruby}}==
Line 1,338 ⟶ 1,830:
=={{header|Swift}}==
{{trans|C}}
I have started working on Swift and am going to practice on RosettaCode. On converting my C implementation for this task to Swift which I contributed last year, I found Swift allows statement ending semicolons and enclosing parantheses in if/else statements which are mandatory in C. I didn't find that anywhere while learning Swift so I am posting both implementations here, the "C like" and the "Pure" Swift.
 
Both have been tested with Xcode 14.2 (14C18)
Line 1,426 ⟶ 1,918:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var F = Fn.new { |n, x, y|
1,150

edits