Sudan function: Difference between revisions
m
syntax highlighting fixup automation
m (oops, remove extra header) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 23:
=={{header|Ada}}==
{{trans|Javascript}}
<
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;</
{{out}}
<pre>
Line 55:
{{Trans|Wren}}
...with a minor optimisation.
<
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</
{{out}}
<pre>
Line 111:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUDAN_FUNCTION.AWK
BEGIN {
Line 144:
return(new_str)
}
</syntaxhighlight>
{{out}}
<pre>
Line 178:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
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</
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|PureBasic}}===
<
If n = 0
ProcedureReturn x + y
Line 235:
PrintN("F(2,2,1) = " + Str(F(2,2,1)))
Input()
CloseConsole()</
{{out}}
<pre>Similat to FreeBASIC entry.</pre>
Line 241:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<
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</
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 270:
=={{header|C}}==
{{trans|Javascript}}
<syntaxhighlight lang="c">
//Aamrun, 11th July 2022
Line 291:
return 0;
}
</syntaxhighlight>
Output
Line 300:
=={{header|C++}}==
{{trans|C}}
<
//Aamrun , 11th July, 2022
Line 322:
return 0;
}
</syntaxhighlight>
Output
<pre>
Line 329:
=={{header|C Sharp|C#}}==
{{trans|C}}
<
//Aamrun, 11th July 2022
Line 356:
}
}
</syntaxhighlight>
Output
<pre>
Line 364:
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<
: sudan ( n x y -- z )
Line 377:
} cond ;
3 1 1 sudan .</
{{out}}
<pre>
Line 384:
Or with locals:
<
:: sudan ( n x y -- z )
Line 391:
{ [ y zero? ] [ x ] }
[ n 1 - n x y 1 - sudan dup y + sudan ]
} cond ;</
=={{header|FreeBASIC}}==
{{trans|Wren and Phyton}}
<
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</
{{out}}
<pre>Same as Wren entry.</pre>
Line 425:
=={{header|Go}}==
{{trans|Wren}}
<
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))
}</
{{out}}
Line 464:
=={{header|Haskell}}==
<
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"</
{{out}}
<pre>
Line 534:
=={{header|Hoon}}==
<syntaxhighlight 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>
=={{header|J}}==
{{trans|Javascript}}
This is, of course, not particularly efficient and some results are too large for a computer to represent.
<
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</
Examples: <
0
F 1 1 1
Line 564:
10228
F 2 2 1
27</
=={{header|Java}}==
{{trans|C}}
<syntaxhighlight lang="java">
//Aamrun, 11th July 2022
Line 589:
}
}
</syntaxhighlight>
Output
<pre>
Line 596:
=={{header|Javascript}}==
<syntaxhighlight lang="javascript">
/**
* @param {bigint} n
Line 614:
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}
</syntaxhighlight>
=={{header|Julia}}==
<
@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)))
</
<pre>
sudan(0, 0, 0) = 0
Line 636:
=={{header|Perl}}==
Three ways of doing the same thing.
<
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)
}</
{{out}}
<pre>0 0 0
Line 660:
=={{header|Phix}}==
<!--<
<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>
<!--</
Output same as Wren.
=={{header|PHP}}==
{{trans|C}}
<
#Aamrun , 11th July 2022
Line 706:
echo "F(1,3,3) = " . F(1,3,3);
?>
</syntaxhighlight>
Output
<pre>
Line 714:
=={{header|Python}}==
{{trans|Javascript}}
<syntaxhighlight lang="python">
# Aamrun, 11th July 2022
Line 727:
print("F(1,3,3) = ", F(1,3,3))
</syntaxhighlight>
Output
Line 736:
=={{header|R}}==
{{trans|C}}
<
#Aamrun, 11th July 2022
Line 755:
print(paste("F(1,3,3) = " , F(1,3,3)))
</syntaxhighlight>
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"
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>
{{out}}
{|class="wikitable"
Line 828:
=={{header|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>
Output
Line 845:
=={{header|Vlang}}==
{{trans|Wren}}
<
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)}")
}</
{{out}}
Line 882:
=={{header|Wren}}==
{{libheader|Wren-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))")</
{{out}}
Line 939:
=={{header|XPL0}}==
{{trans|Wren}}
<
[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);
]</
{{out}}
|