Sudan function: Difference between revisions

m
syntax highlighting fixup automation
m (oops, remove extra header)
m (syntax highlighting fixup automation)
Line 23:
=={{header|Ada}}==
{{trans|Javascript}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Sudan_Function is
Line 41:
Put_Line ("F2 (2, 1) = " & F (2, 2, 1)'Image);
Put_Line ("F3 (1, 1) = " & F (3, 1, 1)'Image);
end Sudan_Function;</langsyntaxhighlight>
{{out}}
<pre>
Line 55:
{{Trans|Wren}}
...with a minor optimisation.
<langsyntaxhighlight lang="algol68">BEGIN # compute some values of the Sudan function #
PROC sudan = ( INT n, x, y )INT:
IF n = 0 THEN x + y
Line 80:
print( ( "F(3, 1, 1) = ", whole( sudan( 3, 1, 1 ), 0 ), newline ) );
print( ( "F(2, 2, 1) = ", whole( sudan( 2, 2, 1 ), 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 111:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUDAN_FUNCTION.AWK
BEGIN {
Line 144:
return(new_str)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 178:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">for n = 0 to 1
print "Values of F(" & n & ", x, y):"
print "y/x 0 1 2 3 4 5"
Line 201:
if y = 0 then return x
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
end function</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.d F(n.i, x.i, y.i)
If n = 0
ProcedureReturn x + y
Line 235:
PrintN("F(2,2,1) = " + Str(F(2,2,1)))
Input()
CloseConsole()</langsyntaxhighlight>
{{out}}
<pre>Similat to FreeBASIC entry.</pre>
Line 241:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">for n = 0 to 1
print "Values of F(", n, ", x, y):"
print "y/x 0 1 2 3 4 5"
Line 264:
if y = 0 return x
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
end sub</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 270:
=={{header|C}}==
{{trans|Javascript}}
<syntaxhighlight lang="c">
<lang C>
//Aamrun, 11th July 2022
 
Line 291:
return 0;
}
</syntaxhighlight>
</lang>
 
Output
Line 300:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">
//Aamrun , 11th July, 2022
 
Line 322:
return 0;
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 329:
=={{header|C Sharp|C#}}==
{{trans|C}}
<langsyntaxhighlight lang="csharp">
//Aamrun, 11th July 2022
 
Line 356:
}
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 364:
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<langsyntaxhighlight lang="factor">USING: combinators kernel math prettyprint ;
 
: sudan ( n x y -- z )
Line 377:
} cond ;
 
3 1 1 sudan .</langsyntaxhighlight>
{{out}}
<pre>
Line 384:
 
Or with locals:
<langsyntaxhighlight lang="factor">USING: combinators kernel locals math prettyprint ;
 
:: sudan ( n x y -- z )
Line 391:
{ [ y zero? ] [ x ] }
[ n 1 - n x y 1 - sudan dup y + sudan ]
} cond ;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|Wren and Phyton}}
<langsyntaxhighlight lang="freebasic">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
Line 419:
Print "F(3,1,1) ="; F(3,1,1)
Print "F(2,2,1) ="; F(2,2,1)
Sleep</langsyntaxhighlight>
{{out}}
<pre>Same as Wren entry.</pre>
Line 425:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 456:
fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}</langsyntaxhighlight>
 
{{out}}
Line 464:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad.Memo (Memo, memo, startEvalMemo)
import Data.List.Split (chunksOf)
import System.Environment (getArgs)
Line 500:
[read xlo .. read xhi]
[read ylo .. read yhi]
_ -> error "Usage: sudan n xmin xmax ymin ymax"</langsyntaxhighlight>
{{out}}
<pre>
Line 534:
 
=={{header|Hoon}}==
<syntaxhighlight lang="hoon">
<lang Hoon>
|= [n=@ x=@ y=@]
^- @
Line 543:
x
$(n (dec n), x $(n n, x x, y (dec y)), y (add $(n n, x x, y (dec y)) y))
</syntaxhighlight>
</lang>
 
=={{header|J}}==
{{trans|Javascript}}
This is, of course, not particularly efficient and some results are too large for a computer to represent.
<langsyntaxhighlight Jlang="j">F=: {{ 'N X Y'=. y assert. N>:0
if. 0=N do. X+Y
elseif. Y=0 do. X
else. F (N-1),(F N,X,Y-1), Y+F N, X, Y-1
end.
}}"1</langsyntaxhighlight>
 
Examples: <langsyntaxhighlight Jlang="j"> F 0 0 0
0
F 1 1 1
Line 564:
10228
F 2 2 1
27</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
<syntaxhighlight lang="java">
<lang Java>
//Aamrun, 11th July 2022
 
Line 589:
}
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 596:
 
=={{header|Javascript}}==
<syntaxhighlight lang="javascript">
<lang Javascript>
/**
* @param {bigint} n
Line 614:
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="ruby">using Memoize
 
@memoize function sudan(n, x, y)
Line 625:
foreach(t -> println("sudan($(t[1]), $(t[2]), $(t[3])) = ",
sudan(t[1], t[2], t[3])), ((0,0,0), (1,1,1), (2,1,1), (3,1,1), (2,2,1)))
</langsyntaxhighlight>{{out}}
<pre>
sudan(0, 0, 0) = 0
Line 636:
=={{header|Perl}}==
Three ways of doing the same thing.
<langsyntaxhighlight lang="perl">use v5.36;
use experimental 'for_list';
 
Line 651:
for my($n,$x,$y) (0,0,0, 1,1,1, 2,1,1, 3,1,1, 2,2,1) {
say join ' ',F1($n,$x,$y), F2($n,$x,$y), F3($n,$x,$y)
}</langsyntaxhighlight>
{{out}}
<pre>0 0 0
Line 660:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">F</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
Line 684:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"F(%d, %d, %d) = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">F</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Output same as Wren.
 
=={{header|PHP}}==
{{trans|C}}
<langsyntaxhighlight lang="php">
#Aamrun , 11th July 2022
 
Line 706:
echo "F(1,3,3) = " . F(1,3,3);
?>
</syntaxhighlight>
</lang>
Output
<pre>
Line 714:
=={{header|Python}}==
{{trans|Javascript}}
<syntaxhighlight lang="python">
<lang Python>
# Aamrun, 11th July 2022
 
Line 727:
print("F(1,3,3) = ", F(1,3,3))
</syntaxhighlight>
</lang>
 
Output
Line 736:
=={{header|R}}==
{{trans|C}}
<langsyntaxhighlight lang="rsplus">
#Aamrun, 11th July 2022
 
Line 755:
 
print(paste("F(1,3,3) = " , F(1,3,3)))
</syntaxhighlight>
</lang>
Output
<pre>
Line 763:
=={{header|Raku}}==
Outputting wiki-tables to more closely emulate the wikipedia examples. Not very efficient but good enough.
<syntaxhighlight lang="raku" perl6line>multi F (0, $x, $y) { $x + $y }
multi F ($n where * > 0, $x, 0) { $x }
multi F ($n, $x, $y) { F($n-1, F($n, $x, $y-1), F($n, $x, $y-1) + $y) }
Line 774:
say( "|}" );
}
</syntaxhighlight>
</lang>
{{out}}
{|class="wikitable"
Line 828:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
def sudan(n, x, y)
return x + y if n == 0
Line 835:
sudan(n - 1, sudan(n, x, y - 1), sudan(n, x, y - 1) + y)
end
</syntaxhighlight>
</lang>
 
Output
Line 845:
=={{header|Vlang}}==
{{trans|Wren}}
<langsyntaxhighlight lang="ruby">fn sudan(n int, x int, y int) int {
if n == 0 {
return x + y
Line 873:
println("F(3, 1, 1) = ${sudan(3, 1, 1)}")
println("F(2, 2, 1) = ${sudan(2, 2, 1)}")
}</langsyntaxhighlight>
 
{{out}}
Line 882:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var F = Fn.new { |n, x, y|
Line 906:
System.print("F(2, 1, 1) = %(F.call(2, 1, 1))")
System.print("F(3, 1, 1) = %(F.call(3, 1, 1))")
System.print("F(2, 2, 1) = %(F.call(2, 2, 1))")</langsyntaxhighlight>
 
{{out}}
Line 939:
=={{header|XPL0}}==
{{trans|Wren}}
<langsyntaxhighlight XPL0lang="xpl0">func F; int N, X, Y;
[if N = 0 then return X + Y;
if Y = 0 then return X;
Line 962:
Text(0, "F(3, 1, 1) = "); IntOut(0, F(3, 1, 1)); CrLf(0);
Text(0, "F(2, 2, 1) = "); IntOut(0, F(2, 2, 1)); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits