Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
6
Task/Sudan-function/00-META.yaml
Normal file
6
Task/Sudan-function/00-META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Memoization
|
||||
- Classic CS problems and programs
|
||||
from: http://rosettacode.org/wiki/Sudan_function
|
||||
note: Recursion
|
||||
14
Task/Sudan-function/00-TASK.txt
Normal file
14
Task/Sudan-function/00-TASK.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
The '''[[wp:Sudan function|Sudan function]]''' is a classic example of a recursive function, notable especially because it is not a [[wp:Primitive_recursive_function|primitive recursive function]]. This is also true of the better-known [[Ackermann function]]. The Sudan function was the first function having this property to be published.
|
||||
|
||||
The Sudan function is usually defined as follows ([https://wikimedia.org/api/rest_v1/media/math/render/svg/59304f0b3ff0baad7dd21f5ce16ce6fa241111a2 svg]):
|
||||
|
||||
<math>\begin{array}{lll}
|
||||
F_0 (x, y) & = x+y \\
|
||||
F_{n+1} (x, 0) & = x & \text{if } n \ge 0 \\
|
||||
F_{n+1} (x, y+1) & = F_n (F_{n+1} (x, y), F_{n+1} (x, y) + y + 1) & \text{if } n\ge 0 \\
|
||||
\end{array}
|
||||
</math>
|
||||
|
||||
;Task:
|
||||
Write a function which returns the value of F(x, y).
|
||||
|
||||
60
Task/Sudan-function/8080-Assembly/sudan-function.8080
Normal file
60
Task/Sudan-function/8080-Assembly/sudan-function.8080
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
org 100h
|
||||
jmp demo
|
||||
;; Sudan function. BC=N, DE=X, HL=Y; output in HL.
|
||||
sudan: mov a,b ; N=0?
|
||||
ora c
|
||||
jz sdnbse
|
||||
mov a,h ; Y=0?
|
||||
ora l
|
||||
jz sdnbse
|
||||
push b ; Save N and Y (we don't need X)
|
||||
push h
|
||||
dcx h
|
||||
call sudan ; Calculate result of inner call
|
||||
xchg ; Set X = result of inner call
|
||||
pop h ; Get Y
|
||||
dad d ; Set Y = Y + result of inner call
|
||||
pop b ; Get N
|
||||
dcx b ; N = N-1
|
||||
jmp sudan ; Calculate result of outer call
|
||||
sdnbse: dad d ; Return X+Y (base case)
|
||||
ret
|
||||
|
||||
;; Output routine (show 'sudan(N,X,Y) = value'), using CP/M call
|
||||
show: push h! push d! push b! ; Copies of args
|
||||
push h! push d! push b! ; For output
|
||||
lxi d,sdt! call pstr ; Print call
|
||||
pop h! call prhl
|
||||
lxi d,sdc! call pstr
|
||||
pop h! call prhl
|
||||
lxi d,sdc! call pstr
|
||||
pop h! call prhl
|
||||
lxi d,sdi! call pstr
|
||||
pop b! pop d! pop h! ; Restore args
|
||||
call sudan! call prhl ; Find and print result
|
||||
lxi d,sdnl! jmp pstr
|
||||
prhl: lxi d,numbuf! push d! lxi b,-10
|
||||
prdgt: lxi d,-1
|
||||
pdiv: inx d! dad b! jc pdiv
|
||||
mvi a,'0'+10! add l! pop h
|
||||
dcx h! mov m,a! push h
|
||||
xchg! mov a,h! ora l! jnz prdgt
|
||||
pop d
|
||||
pstr: mvi c,9! jmp 5
|
||||
|
||||
;; Set up big system stack (using CP/M)
|
||||
demo: lhld 6
|
||||
sphl
|
||||
;; Show a couple of values
|
||||
lxi b,0! lxi d,0! lxi h,0! call show
|
||||
lxi b,1! lxi d,1! lxi h,1! call show
|
||||
lxi b,2! lxi d,1! lxi h,1! call show
|
||||
lxi b,3! lxi d,1! lxi h,1! call show
|
||||
lxi b,2! lxi d,2! lxi h,1! call show
|
||||
rst 0
|
||||
sdt: db 'sudan($'
|
||||
sdc: db ', $'
|
||||
sdi: db ') = $'
|
||||
db '.....'
|
||||
numbuf: db '$'
|
||||
sdnl: db 13,10,'$'
|
||||
26
Task/Sudan-function/ALGOL-68/sudan-function.alg
Normal file
26
Task/Sudan-function/ALGOL-68/sudan-function.alg
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
BEGIN # compute some values of the Sudan function #
|
||||
PROC sudan = ( INT n, x, y )INT:
|
||||
IF n = 0 THEN x + y
|
||||
ELIF y = 0 THEN x
|
||||
ELSE
|
||||
INT s = sudan( n, x, y - 1 );
|
||||
sudan( n - 1, s, s + y )
|
||||
FI # sudan # ;
|
||||
FOR n FROM 0 TO 1 DO
|
||||
print( ( "Values of F(", whole( n, 0 ), ", x, y):", newline ) );
|
||||
print( ( "y/x 0 1 2 3 4 5", newline ) );
|
||||
print( ( "----------------------------", newline ) );
|
||||
FOR y FROM 0 TO 6 DO
|
||||
print( ( whole( y, 0 ), " |" ) );
|
||||
FOR x FROM 0 TO 5 DO
|
||||
print( ( whole( sudan( n, x, y ), -4 ) ) )
|
||||
OD;
|
||||
print( ( newline ) )
|
||||
OD;
|
||||
print( ( newline ) )
|
||||
OD;
|
||||
print( ( newline ) );
|
||||
print( ( "F(2, 1, 1) = ", whole( sudan( 2, 1, 1 ), 0 ), newline ) );
|
||||
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
|
||||
6
Task/Sudan-function/APL/sudan-function.apl
Normal file
6
Task/Sudan-function/APL/sudan-function.apl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
sudan←{
|
||||
0∨.>⍺ ⍺⍺ ⍵:'Negative input'⎕SIGNAL 11
|
||||
⍺⍺=0:⍺+⍵
|
||||
⍵=0:⍺
|
||||
tm((⍺⍺-1)∇∇)⍵+tm←⍺∇⍵-1
|
||||
}
|
||||
32
Task/Sudan-function/AWK/sudan-function.awk
Normal file
32
Task/Sudan-function/AWK/sudan-function.awk
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# syntax: GAWK -f SUDAN_FUNCTION.AWK
|
||||
BEGIN {
|
||||
for (n=0; n<=1; n++) {
|
||||
printf("sudan(%d,x,y)\n",n)
|
||||
printf("y/x 0 1 2 3 4 5\n")
|
||||
printf("%s\n",strdup("-",28))
|
||||
for (y=0; y<=6; y++) {
|
||||
printf("%2d | ",y)
|
||||
for (x=0; x<=5; x++) {
|
||||
printf("%3d ",sudan(n,x,y))
|
||||
}
|
||||
printf("\n")
|
||||
}
|
||||
printf("\n")
|
||||
}
|
||||
n=1; x=3; y=3; printf("sudan(%d,%d,%d)=%d\n",n,x,y,sudan(n,x,y))
|
||||
n=2; x=1; y=1; printf("sudan(%d,%d,%d)=%d\n",n,x,y,sudan(n,x,y))
|
||||
n=2; x=2; y=1; printf("sudan(%d,%d,%d)=%d\n",n,x,y,sudan(n,x,y))
|
||||
n=3; x=1; y=1; printf("sudan(%d,%d,%d)=%d\n",n,x,y,sudan(n,x,y))
|
||||
exit(0)
|
||||
}
|
||||
function sudan(n,x,y) {
|
||||
if (n == 0) { return(x+y) }
|
||||
if (y == 0) { return(x) }
|
||||
return sudan(n-1, sudan(n,x,y-1), sudan(n,x,y-1)+y)
|
||||
}
|
||||
function strdup(str,n, i,new_str) {
|
||||
for (i=1; i<=n; i++) {
|
||||
new_str = new_str str
|
||||
}
|
||||
return(new_str)
|
||||
}
|
||||
19
Task/Sudan-function/Ada/sudan-function.ada
Normal file
19
Task/Sudan-function/Ada/sudan-function.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Sudan_Function is
|
||||
|
||||
function F (N, X, Y : Natural) return Natural
|
||||
is (if N = 0 then X + Y
|
||||
elsif Y = 0 then X
|
||||
else F (N => N - 1,
|
||||
X => F (N, X, Y - 1),
|
||||
Y => F (N, X, Y - 1) + Y));
|
||||
|
||||
begin
|
||||
Put_Line ("F0 (0, 0) = " & F (0, 0, 0)'Image);
|
||||
Put_Line ("F1 (1, 1) = " & F (1, 1, 1)'Image);
|
||||
Put_Line ("F1 (3, 3) = " & F (1, 3, 3)'Image);
|
||||
Put_Line ("F2 (1, 1) = " & F (2, 1, 1)'Image);
|
||||
Put_Line ("F2 (2, 1) = " & F (2, 2, 1)'Image);
|
||||
Put_Line ("F3 (1, 1) = " & F (3, 1, 1)'Image);
|
||||
end Sudan_Function;
|
||||
8
Task/Sudan-function/Arturo/sudan-function.arturo
Normal file
8
Task/Sudan-function/Arturo/sudan-function.arturo
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
24
Task/Sudan-function/BASIC256/sudan-function.basic
Normal file
24
Task/Sudan-function/BASIC256/sudan-function.basic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
for n = 0 to 1
|
||||
print "Values of F(" & n & ", x, y):"
|
||||
print "y/x 0 1 2 3 4 5"
|
||||
print ("-"*28)
|
||||
for y = 0 to 6
|
||||
print y; " |";
|
||||
for x = 0 to 5
|
||||
print rjust(string(F(n,x,y)),4);
|
||||
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 F(n, x, y)
|
||||
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
|
||||
30
Task/Sudan-function/BCPL/sudan-function.bcpl
Normal file
30
Task/Sudan-function/BCPL/sudan-function.bcpl
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
get "libhdr"
|
||||
|
||||
let sudan(n, x, y) =
|
||||
n = 0 -> x + y,
|
||||
y = 0 -> x,
|
||||
sudan(n-1, sudan(n, x, y-1), sudan(n, x, y-1)+y)
|
||||
|
||||
let showtable(f, n, x, y) be
|
||||
$( writef("sudan(%N,x,y)*N", n)
|
||||
writes(" ")
|
||||
for i=0 to x do writed(i, 5)
|
||||
for i=0 to y
|
||||
$( writef("*N%I4:", i)
|
||||
for j=0 to x do writed(f(n, j, i), 5)
|
||||
$)
|
||||
writes("*N*N")
|
||||
$)
|
||||
|
||||
let show(f, n, x, y) be
|
||||
writef("sudan(%I4,%I4,%I4) = %I6*N", n, x, y, f(n, x, y))
|
||||
|
||||
let start() be
|
||||
$( showtable(sudan, 0, 6, 5)
|
||||
showtable(sudan, 1, 6, 5)
|
||||
wrch('*N')
|
||||
show(sudan, 1, 3, 3)
|
||||
show(sudan, 2, 1, 1)
|
||||
show(sudan, 2, 2, 1)
|
||||
show(sudan, 3, 1, 1)
|
||||
$)
|
||||
15
Task/Sudan-function/BQN/sudan-function.bqn
Normal file
15
Task/Sudan-function/BQN/sudan-function.bqn
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
_sudan←{
|
||||
x 0 _𝕣 y: x + y;
|
||||
x n _𝕣 0: x;
|
||||
x n _𝕣 y: k (n-1)_𝕣 y+k←x𝕊y-1
|
||||
}
|
||||
|
||||
•Show "⍉(↕7) 0 _sudan⌜ ↕6:"
|
||||
•Show ⍉(↕7) 0 _sudan⌜ ↕6
|
||||
|
||||
•Show "⍉(↕7) 1 _sudan⌜ ↕6:"
|
||||
•Show ⍉(↕7) 1 _sudan⌜ ↕6
|
||||
|
||||
•Show "1 2 _sudan 1: "∾•Fmt 1 2 _sudan 1
|
||||
•Show "2 2 _sudan 1: "∾•Fmt 2 2 _sudan 1
|
||||
•Show "1 3 _sudan 1: "∾•Fmt 1 3 _sudan 1
|
||||
21
Task/Sudan-function/C++/sudan-function.cpp
Normal file
21
Task/Sudan-function/C++/sudan-function.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
//Aamrun , 11th July, 2022
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
|
||||
return 0;
|
||||
}
|
||||
26
Task/Sudan-function/C-sharp/sudan-function.cs
Normal file
26
Task/Sudan-function/C-sharp/sudan-function.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//Aamrun, 11th July 2022
|
||||
|
||||
using System;
|
||||
|
||||
namespace Sudan
|
||||
{
|
||||
class Sudan
|
||||
{
|
||||
static 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);
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("F(1,3,3) = " + F(1,3,3));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Task/Sudan-function/C/sudan-function.c
Normal file
20
Task/Sudan-function/C/sudan-function.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
//Aamrun, 11th July 2022
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("F1(3,3) = %d",F(1,3,3));
|
||||
return 0;
|
||||
}
|
||||
43
Task/Sudan-function/CLU/sudan-function.clu
Normal file
43
Task/Sudan-function/CLU/sudan-function.clu
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
sudan = proc (n, x, y: int) returns (int)
|
||||
if n=0 then
|
||||
return(x + y)
|
||||
elseif y=0 then
|
||||
return(x)
|
||||
else
|
||||
k: int := sudan(n, x, y-1)
|
||||
return(sudan(n-1, k, k+y))
|
||||
end
|
||||
end sudan
|
||||
|
||||
table = proc (n, xs, ys: int) returns (string)
|
||||
ss: stream := stream$create_output()
|
||||
stream$putl(ss, "sudan(" || int$unparse(n) || ",x,y):")
|
||||
stream$puts(ss, " ")
|
||||
for x: int in int$from_to(0, xs)
|
||||
do stream$putright(ss, int$unparse(x), 5) end
|
||||
for y: int in int$from_to(0, ys) do
|
||||
stream$putl(ss, "")
|
||||
stream$putright(ss, int$unparse(y) || ":", 5)
|
||||
for x: int in int$from_to(0, xs)
|
||||
do stream$putright(ss, int$unparse(sudan(n, x, y)), 5) end
|
||||
end
|
||||
stream$putl(ss, "")
|
||||
return(stream$get_contents(ss))
|
||||
end table
|
||||
|
||||
show = proc (n, x, y: int) returns (string)
|
||||
return("sudan(" || int$unparse(n)
|
||||
|| ", " || int$unparse(x)
|
||||
|| ", " || int$unparse(y)
|
||||
|| ") = " || int$unparse(sudan(n,x,y)))
|
||||
end show
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
stream$putl(po, table(0, 6, 5))
|
||||
stream$putl(po, table(1, 6, 5))
|
||||
stream$putl(po, show(1, 3, 3))
|
||||
stream$putl(po, show(2, 1, 1))
|
||||
stream$putl(po, show(2, 2, 1))
|
||||
stream$putl(po, show(3, 1, 1))
|
||||
end start_up
|
||||
13
Task/Sudan-function/Dart/sudan-function.dart
Normal file
13
Task/Sudan-function/Dart/sudan-function.dart
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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)}');
|
||||
}
|
||||
40
Task/Sudan-function/Delphi/sudan-function.delphi
Normal file
40
Task/Sudan-function/Delphi/sudan-function.delphi
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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;
|
||||
38
Task/Sudan-function/Draco/sudan-function.draco
Normal file
38
Task/Sudan-function/Draco/sudan-function.draco
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
proc sudan(word n, x, y) word:
|
||||
word k;
|
||||
if n=0 then
|
||||
x + y
|
||||
elif y=0 then
|
||||
x
|
||||
else
|
||||
k := sudan(n, x, y-1);
|
||||
sudan(n-1, k, k+y)
|
||||
fi
|
||||
corp
|
||||
|
||||
proc table(word n, xs, ys) void:
|
||||
word x, y;
|
||||
writeln("sudan(",n,",x,y):");
|
||||
write(" ");
|
||||
for x from 0 upto xs do write(x:5) od;
|
||||
for y from 0 upto ys do
|
||||
writeln();
|
||||
write(y:4, ":");
|
||||
for x from 0 upto xs do write(sudan(n,x,y):5) od;
|
||||
od;
|
||||
writeln();
|
||||
writeln()
|
||||
corp
|
||||
|
||||
proc show(word n, x, y) void:
|
||||
writeln("sudan(", n:1, ",", x:3, ",", y:3, ") = ", sudan(n,x,y):5)
|
||||
corp
|
||||
|
||||
proc main() void:
|
||||
table(0, 6, 5);
|
||||
table(1, 6, 5);
|
||||
show(1, 3, 3);
|
||||
show(2, 1, 1);
|
||||
show(2, 2, 1);
|
||||
show(3, 1, 1)
|
||||
corp
|
||||
5
Task/Sudan-function/F-Sharp/sudan-function.fs
Normal file
5
Task/Sudan-function/F-Sharp/sudan-function.fs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
let rec sudan = function
|
||||
0L, x, y -> x + y
|
||||
|_, x, 0L -> x
|
||||
|n, x, y -> let x' = sudan (n, x, y-1L) in sudan (n-1L, x', x' + y)
|
||||
printfn "%d\n%d\n%d" (sudan(1L, 13L, 14L)) (sudan(2L, 5L, 1L)) (sudan(2L, 2L, 2L))
|
||||
14
Task/Sudan-function/Factor/sudan-function-1.factor
Normal file
14
Task/Sudan-function/Factor/sudan-function-1.factor
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
USING: combinators kernel math prettyprint ;
|
||||
|
||||
: sudan ( n x y -- z )
|
||||
{
|
||||
{ [ pick zero? ] [ nipd + ] }
|
||||
{ [ dup zero? ] [ drop nip ] }
|
||||
[
|
||||
[ 2drop 1 - ]
|
||||
[ 1 - sudan dup ]
|
||||
[ 2nip + sudan ] 3tri
|
||||
]
|
||||
} cond ;
|
||||
|
||||
3 1 1 sudan .
|
||||
8
Task/Sudan-function/Factor/sudan-function-2.factor
Normal file
8
Task/Sudan-function/Factor/sudan-function-2.factor
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
USING: combinators kernel locals math prettyprint ;
|
||||
|
||||
:: sudan ( n x y -- z )
|
||||
{
|
||||
{ [ n zero? ] [ x y + ] }
|
||||
{ [ y zero? ] [ x ] }
|
||||
[ n 1 - n x y 1 - sudan dup y + sudan ]
|
||||
} cond ;
|
||||
54
Task/Sudan-function/Free-Pascal-Lazarus/sudan-function.pas
Normal file
54
Task/Sudan-function/Free-Pascal-Lazarus/sudan-function.pas
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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.
|
||||
25
Task/Sudan-function/FreeBASIC/sudan-function.basic
Normal file
25
Task/Sudan-function/FreeBASIC/sudan-function.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
|
||||
Dim As Integer n, x, y
|
||||
For n = 0 To 1
|
||||
Print " Values of F(" & n & ", x, y):"
|
||||
Print " y/x 0 1 2 3 4 5"
|
||||
Print String(29, "-")
|
||||
For y = 0 To 6
|
||||
Print y; " |";
|
||||
For x = 0 To 5
|
||||
Print Using "####"; 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)
|
||||
Sleep
|
||||
31
Task/Sudan-function/Gambas/sudan-function.gambas
Normal file
31
Task/Sudan-function/Gambas/sudan-function.gambas
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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
|
||||
32
Task/Sudan-function/Go/sudan-function.go
Normal file
32
Task/Sudan-function/Go/sudan-function.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func F(n, x, y int) int {
|
||||
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)
|
||||
}
|
||||
|
||||
func main() {
|
||||
for n := 0; n < 2; n++ {
|
||||
fmt.Printf("Values of F(%d, x, y):\n", n)
|
||||
fmt.Println("y/x 0 1 2 3 4 5")
|
||||
fmt.Println("----------------------------")
|
||||
for y := 0; y < 7; y++ {
|
||||
fmt.Printf("%d |", y)
|
||||
for x := 0; x < 6; x++ {
|
||||
fmt.Printf("%4d", F(n, x, y))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
|
||||
fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
|
||||
fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
|
||||
}
|
||||
37
Task/Sudan-function/Haskell/sudan-function.hs
Normal file
37
Task/Sudan-function/Haskell/sudan-function.hs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import Control.Monad.Memo (Memo, memo, startEvalMemo)
|
||||
import Data.List.Split (chunksOf)
|
||||
import System.Environment (getArgs)
|
||||
import Text.Tabular (Header(..), Properties(..), Table(..))
|
||||
import Text.Tabular.AsciiArt (render)
|
||||
|
||||
type SudanArgs = (Int, Integer, Integer)
|
||||
|
||||
-- Given argument (n, x, y) calculate Fₙ(x, y). For performance reasons we do
|
||||
-- the calculation in a memoization monad.
|
||||
sudan :: SudanArgs -> Memo SudanArgs Integer Integer
|
||||
sudan (0, x, y) = return $ x + y
|
||||
sudan (_, x, 0) = return x
|
||||
sudan (n, x, y) = memo sudan (n, x, y-1) >>= \x2 -> sudan (n-1, x2, x2 + y)
|
||||
|
||||
-- A table of Fₙ(x, y) values, where the rows are y values and the columns are
|
||||
-- x values.
|
||||
sudanTable :: Int -> [Integer] -> [Integer] -> String
|
||||
sudanTable n xs ys = render show show show
|
||||
$ Table (Group NoLine $ map Header ys)
|
||||
(Group NoLine $ map Header xs)
|
||||
$ chunksOf (length xs)
|
||||
$ startEvalMemo
|
||||
$ sequence
|
||||
$ [sudan (n, x, y) | y <- ys, x <- xs]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
case args of
|
||||
[n, xlo, xhi, ylo, yhi] -> do
|
||||
putStrLn $ "Fₙ(x, y), where the rows are y values " ++
|
||||
"and the columns are x values.\n"
|
||||
putStr $ sudanTable (read n)
|
||||
[read xlo .. read xhi]
|
||||
[read ylo .. read yhi]
|
||||
_ -> error "Usage: sudan n xmin xmax ymin ymax"
|
||||
8
Task/Sudan-function/Hoon/sudan-function.hoon
Normal file
8
Task/Sudan-function/Hoon/sudan-function.hoon
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|= [n=@ x=@ y=@]
|
||||
^- @
|
||||
|-
|
||||
?: =(n 0)
|
||||
(add x y)
|
||||
?: =(y 0)
|
||||
x
|
||||
$(n (dec n), x $(n n, x x, y (dec y)), y (add $(n n, x x, y (dec y)) y))
|
||||
6
Task/Sudan-function/J/sudan-function-1.j
Normal file
6
Task/Sudan-function/J/sudan-function-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
10
Task/Sudan-function/J/sudan-function-2.j
Normal file
10
Task/Sudan-function/J/sudan-function-2.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
F 0 0 0
|
||||
0
|
||||
F 1 1 1
|
||||
3
|
||||
F 2 1 1
|
||||
8
|
||||
F 3 1 1
|
||||
10228
|
||||
F 2 2 1
|
||||
27
|
||||
20
Task/Sudan-function/Java/sudan-function.java
Normal file
20
Task/Sudan-function/Java/sudan-function.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
//Aamrun, 11th July 2022
|
||||
|
||||
public class Main {
|
||||
|
||||
private static 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);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("F(1,3,3) = " + F(1,3,3));
|
||||
}
|
||||
}
|
||||
17
Task/Sudan-function/JavaScript/sudan-function.js
Normal file
17
Task/Sudan-function/JavaScript/sudan-function.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* @param {bigint} n
|
||||
* @param {bigint} x
|
||||
* @param {bigint} y
|
||||
* @returns {bigint}
|
||||
*/
|
||||
function 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);
|
||||
}
|
||||
13
Task/Sudan-function/Jq/sudan-function.jq
Normal file
13
Task/Sudan-function/Jq/sudan-function.jq
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def sudan(n;x;y):
|
||||
if n == 0 then x+y
|
||||
elif y == 0 then x
|
||||
else sudan(n-1; sudan(n;x;y-1); sudan(n;x;y-1) + y)
|
||||
end;
|
||||
|
||||
# For testing and syntactic convenience:
|
||||
def sudan:
|
||||
"sudan(\(.[0]); \(.[1]); \(.[2])) => \(sudan(.[0]; .[1]; .[2]))";
|
||||
|
||||
# Illustrations
|
||||
[0,0,0], [1,1,1], [2,1,1], [3,1,1], [2,2,1]
|
||||
| sudan
|
||||
8
Task/Sudan-function/Julia/sudan-function.julia
Normal file
8
Task/Sudan-function/Julia/sudan-function.julia
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using Memoize
|
||||
|
||||
@memoize function sudan(n, x, y)
|
||||
return n == 0 ? x + y : y == 0 ? x : sudan(n - 1, sudan(n, x, y - 1), sudan(n, x, y - 1) + y)
|
||||
end
|
||||
|
||||
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)))
|
||||
23
Task/Sudan-function/Lua/sudan-function.lua
Normal file
23
Task/Sudan-function/Lua/sudan-function.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
39
Task/Sudan-function/MAD/sudan-function.mad
Normal file
39
Task/Sudan-function/MAD/sudan-function.mad
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
NORMAL MODE IS INTEGER
|
||||
|
||||
R WE HAVE TO DEFINE OUR OWN STACK FIRST
|
||||
DIMENSION STACK(1000)
|
||||
SET LIST TO STACK
|
||||
|
||||
R SUDAN FUNCTION
|
||||
INTERNAL FUNCTION(N,X,Y)
|
||||
ENTRY TO SUDAN.
|
||||
|
||||
R BASE CASES
|
||||
WHENEVER N.E.0, FUNCTION RETURN X+Y
|
||||
WHENEVER Y.E.0, FUNCTION RETURN X
|
||||
|
||||
R RECURSIVE CASE - WITH MANUAL STACK MANIPULATION
|
||||
R NOTE WE DON'T NEED X AFTER THE FIRST CALL
|
||||
SAVE RETURN
|
||||
SAVE DATA N,Y
|
||||
K = SUDAN.(N,X,Y-1)
|
||||
RESTORE DATA N,Y
|
||||
RESTORE RETURN
|
||||
|
||||
SAVE RETURN
|
||||
K = SUDAN.(N-1, K, K+Y)
|
||||
RESTORE RETURN
|
||||
FUNCTION RETURN K
|
||||
END OF FUNCTION
|
||||
|
||||
INTERNAL FUNCTION(N,X,Y)
|
||||
ENTRY TO SHOW.
|
||||
VECTOR VALUES FMT = $7HSUDAN.(,I1,1H,,I1,1H,,I1,4H) = ,I8*$
|
||||
PRINT FORMAT FMT,N,X,Y,SUDAN.(N,X,Y)
|
||||
END OF FUNCTION
|
||||
|
||||
SHOW.(1,3,3)
|
||||
SHOW.(2,1,1)
|
||||
SHOW.(2,2,1)
|
||||
SHOW.(3,1,1)
|
||||
END OF PROGRAM
|
||||
34
Task/Sudan-function/Modula-2/sudan-function.mod2
Normal file
34
Task/Sudan-function/Modula-2/sudan-function.mod2
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
MODULE Sudan;
|
||||
FROM InOut IMPORT WriteCard, WriteString, WriteLn;
|
||||
|
||||
PROCEDURE sudan(n, x, y: CARDINAL): CARDINAL;
|
||||
VAR k: CARDINAL;
|
||||
BEGIN
|
||||
IF n = 0 THEN RETURN x+y
|
||||
ELSIF y = 0 THEN RETURN x
|
||||
ELSE
|
||||
k := sudan(n, x, y-1);
|
||||
RETURN sudan(n-1, k, k+y)
|
||||
END
|
||||
END sudan;
|
||||
|
||||
PROCEDURE Show(n, x, y: CARDINAL);
|
||||
BEGIN
|
||||
WriteString("sudan(");
|
||||
WriteCard(n, 0);
|
||||
WriteString(", ");
|
||||
WriteCard(x, 0);
|
||||
WriteString(", ");
|
||||
WriteCard(y, 0);
|
||||
WriteString(") = ");
|
||||
WriteCard(sudan(n,x,y), 0);
|
||||
WriteLn
|
||||
END Show;
|
||||
|
||||
BEGIN
|
||||
Show(0, 0, 0);
|
||||
Show(1, 1, 1);
|
||||
Show(2, 1, 1);
|
||||
Show(3, 1, 1);
|
||||
Show(2, 2, 1)
|
||||
END Sudan.
|
||||
16
Task/Sudan-function/Nim/sudan-function.nim
Normal file
16
Task/Sudan-function/Nim/sudan-function.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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)}"
|
||||
4
Task/Sudan-function/OCaml/sudan-function.ocaml
Normal file
4
Task/Sudan-function/OCaml/sudan-function.ocaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
let rec sudan = function
|
||||
| 0, x, y -> x + y
|
||||
| _, x, 0 -> x
|
||||
| n, x, y -> let x' = sudan (n, x, pred y) in sudan (pred n, x', x' + y)
|
||||
16
Task/Sudan-function/PHP/sudan-function.php
Normal file
16
Task/Sudan-function/PHP/sudan-function.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#Aamrun , 11th July 2022
|
||||
|
||||
<?php
|
||||
function 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);
|
||||
}
|
||||
echo "F(1,3,3) = " . F(1,3,3);
|
||||
?>
|
||||
16
Task/Sudan-function/Perl/sudan-function.pl
Normal file
16
Task/Sudan-function/Perl/sudan-function.pl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use v5.36;
|
||||
use experimental 'for_list';
|
||||
|
||||
sub F1($n, $x, $y) { $n ? $y ? F1($n-1, F2($n,$x,$y-1), F3($n,$x,$y-1)+$y) : $x : $x+$y }
|
||||
|
||||
sub F2($n, $x, $y) { $n == 0 ? $x+$y : $y == 0 ? $x : F2($n-1, F1($n,$x,$y-1), F3($n,$x,$y-1)+$y) }
|
||||
|
||||
sub F3($n, $x, $y) {
|
||||
return $x + $y if $n == 0;
|
||||
return $x if $y == 0;
|
||||
F3($n-1, F1($n, $x, $y-1), F2($n, $x, $y-1) + $y)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
25
Task/Sudan-function/Phix/sudan-function.phix
Normal file
25
Task/Sudan-function/Phix/sudan-function.phix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(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>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">x</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</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: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</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;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</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;">function</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<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;">"Values of F(%d, x, y):\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<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;">"y/x 0 1 2 3 4 5\n"</span><span style="color: #0000FF;">)</span>
|
||||
<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;">"----------------------------\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">6</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</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: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">y</span><span style="color: #0000FF;">})</span>
|
||||
<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;">"%d |%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<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;">"\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</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: #0000FF;">=</span> <span style="color: #000000;">t</span>
|
||||
<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>
|
||||
<!--
|
||||
30
Task/Sudan-function/PureBasic/sudan-function.basic
Normal file
30
Task/Sudan-function/PureBasic/sudan-function.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
Procedure.d F(n.i, x.i, y.i)
|
||||
If n = 0
|
||||
ProcedureReturn x + y
|
||||
ElseIf y = 0
|
||||
ProcedureReturn x
|
||||
Else
|
||||
ProcedureReturn F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
OpenConsole()
|
||||
For n = 0 To 1
|
||||
PrintN("Values of F(" + Str(n) + ", x, y):")
|
||||
PrintN("y/x 0 1 2 3 4 5")
|
||||
PrintN("---------------------------------------------------")
|
||||
For y = 0 To 6
|
||||
Print(Str(y) + " |")
|
||||
For x = 0 To 5
|
||||
Print(#TAB$ + F(n,x,y))
|
||||
Next x
|
||||
PrintN("")
|
||||
Next y
|
||||
PrintN("")
|
||||
Next n
|
||||
|
||||
PrintN("F(2,1,1) = " + Str(F(2,1,1)))
|
||||
PrintN("F(3,1,1) = " + Str(F(3,1,1)))
|
||||
PrintN("F(2,2,1) = " + Str(F(2,2,1)))
|
||||
Input()
|
||||
CloseConsole()
|
||||
12
Task/Sudan-function/Python/sudan-function.py
Normal file
12
Task/Sudan-function/Python/sudan-function.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Aamrun, 11th July 2022
|
||||
|
||||
def F(n,x,y):
|
||||
if n==0:
|
||||
return x + y
|
||||
elif y==0:
|
||||
return x
|
||||
else:
|
||||
return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
|
||||
|
||||
|
||||
print("F(1,3,3) = ", F(1,3,3))
|
||||
16
Task/Sudan-function/QBasic/sudan-function.basic
Normal file
16
Task/Sudan-function/QBasic/sudan-function.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
20
Task/Sudan-function/Quackery/sudan-function.quackery
Normal file
20
Task/Sudan-function/Quackery/sudan-function.quackery
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
[ rot dup 0 = iff
|
||||
[ drop + ] done
|
||||
over 0 = iff
|
||||
2drop done
|
||||
over temp put
|
||||
dup 1 -
|
||||
swap 2swap 1 -
|
||||
recurse
|
||||
dup temp take +
|
||||
again ] is sudan ( n x y --> f(n) )
|
||||
|
||||
' [ [ 0 0 0 ]
|
||||
[ 1 1 1 ]
|
||||
[ 1 3 3 ]
|
||||
[ 2 1 1 ]
|
||||
[ 2 2 1 ]
|
||||
[ 3 1 1 ] ]
|
||||
witheach
|
||||
[ dup echo say " = "
|
||||
do sudan echo cr ]
|
||||
18
Task/Sudan-function/R/sudan-function.r
Normal file
18
Task/Sudan-function/R/sudan-function.r
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#Aamrun, 11th July 2022
|
||||
|
||||
F <- function(n, x, y) {
|
||||
if(n==0){
|
||||
F <- x+y
|
||||
return (F)
|
||||
}
|
||||
|
||||
else if(y == 0) {
|
||||
F <- x
|
||||
return (F)
|
||||
}
|
||||
|
||||
F <- F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
|
||||
return (F)
|
||||
}
|
||||
|
||||
print(paste("F(1,3,3) = " , F(1,3,3)))
|
||||
25
Task/Sudan-function/REXX/sudan-function.rexx
Normal file
25
Task/Sudan-function/REXX/sudan-function.rexx
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* 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
|
||||
11
Task/Sudan-function/Raku/sudan-function.raku
Normal file
11
Task/Sudan-function/Raku/sudan-function.raku
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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) }
|
||||
|
||||
# Testing
|
||||
for 0, 6, 1, 15 -> $f, $g {
|
||||
my @range = ^$g;
|
||||
say "\{|class=\"wikitable\"\n", "|+ F\<sub>$f\</sub> (x,y)\n" ~ '!x\y!!', join '!!', @range;
|
||||
-> $r { say "|-\n" ~ '|' ~ join '||', $r, @range.map:{ F($f, $r, $_) } } for @range;
|
||||
say( "|}" );
|
||||
}
|
||||
6
Task/Sudan-function/Ruby/sudan-function.rb
Normal file
6
Task/Sudan-function/Ruby/sudan-function.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def sudan(n, x, y)
|
||||
return x + y if n == 0
|
||||
return x if y == 0
|
||||
|
||||
sudan(n - 1, sudan(n, x, y - 1), sudan(n, x, y - 1) + y)
|
||||
end
|
||||
16
Task/Sudan-function/Run-BASIC/sudan-function.basic
Normal file
16
Task/Sudan-function/Run-BASIC/sudan-function.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
15
Task/Sudan-function/Swift/sudan-function-1.swift
Normal file
15
Task/Sudan-function/Swift/sudan-function-1.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
//Aamrun, 3rd February 2023
|
||||
|
||||
func F(n: Int,x: Int,y: Int) -> Int {
|
||||
if (n == 0) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
else if (y == 0) {
|
||||
return x;
|
||||
}
|
||||
|
||||
return F(n: n - 1, x: F(n: n, x: x, y: y - 1), y: F(n: n, x: x, y: y - 1) + y);
|
||||
}
|
||||
|
||||
print("F1(3,3) = " + String(F(n: 1,x: 3,y: 3)));
|
||||
15
Task/Sudan-function/Swift/sudan-function-2.swift
Normal file
15
Task/Sudan-function/Swift/sudan-function-2.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
//Aamrun, 3rd February 2023
|
||||
|
||||
func F(n: Int,x: Int,y: Int) -> Int {
|
||||
if n == 0 {
|
||||
return x + y
|
||||
}
|
||||
|
||||
else if y == 0 {
|
||||
return x
|
||||
}
|
||||
|
||||
return F(n: n - 1, x: F(n: n, x: x, y: y - 1), y: F(n: n, x: x, y: y - 1) + y)
|
||||
}
|
||||
|
||||
print("F1(3,3) = " + String(F(n: 1,x: 3,y: 3)))
|
||||
16
Task/Sudan-function/True-BASIC/sudan-function.basic
Normal file
16
Task/Sudan-function/True-BASIC/sudan-function.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
29
Task/Sudan-function/V-(Vlang)/sudan-function.v
Normal file
29
Task/Sudan-function/V-(Vlang)/sudan-function.v
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
fn sudan(n int, x int, y int) int {
|
||||
if n == 0 {
|
||||
return x + y
|
||||
}
|
||||
if y == 0 {
|
||||
return x
|
||||
}
|
||||
return sudan(n-1, sudan(n, x, y-1), sudan(n, x, y-1) + y)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for n := 0; n < 2; n++ {
|
||||
println("Values of F($n, x, y):")
|
||||
println("y/x 0 1 2 3 4 5")
|
||||
println("----------------------------")
|
||||
for y := 0; y < 7; y++ {
|
||||
print("$y |")
|
||||
for x := 0; x < 6; x++ {
|
||||
s := sudan(n, x, y)
|
||||
print("${s:4}")
|
||||
}
|
||||
println('')
|
||||
}
|
||||
println('')
|
||||
}
|
||||
println("F(2, 1, 1) = ${sudan(2, 1, 1)}")
|
||||
println("F(3, 1, 1) = ${sudan(3, 1, 1)}")
|
||||
println("F(2, 2, 1) = ${sudan(2, 2, 1)}")
|
||||
}
|
||||
25
Task/Sudan-function/Wren/sudan-function.wren
Normal file
25
Task/Sudan-function/Wren/sudan-function.wren
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import "./fmt" for Fmt
|
||||
|
||||
var F = Fn.new { |n, x, y|
|
||||
if (n == 0) return x + y
|
||||
if (y == 0) return x
|
||||
return F.call(n - 1, F.call(n, x, y-1), F.call(n, x, y-1) + y)
|
||||
}
|
||||
|
||||
for (n in 0..1) {
|
||||
System.print("Values of F(%(n), x, y):")
|
||||
System.print("y/x 0 1 2 3 4 5")
|
||||
System.print("----------------------------")
|
||||
for (y in 0..6) {
|
||||
System.write("%(y) |")
|
||||
for (x in 0..5) {
|
||||
var sudan = F.call(n, x, y)
|
||||
Fmt.write("$4d", sudan)
|
||||
}
|
||||
System.print()
|
||||
}
|
||||
System.print()
|
||||
}
|
||||
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))")
|
||||
40
Task/Sudan-function/XBasic/sudan-function.basic
Normal file
40
Task/Sudan-function/XBasic/sudan-function.basic
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
24
Task/Sudan-function/XPL0/sudan-function.xpl0
Normal file
24
Task/Sudan-function/XPL0/sudan-function.xpl0
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
func F; int N, X, Y;
|
||||
[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);
|
||||
];
|
||||
|
||||
int N, X, Y;
|
||||
[Format(4, 0);
|
||||
for N:= 0 to 1 do
|
||||
[Text(0, "Values of F("); IntOut(0, N); Text(0, ", X, Y):^m^j");
|
||||
Text(0, "Y/X 0 1 2 3 4 5^m^j");
|
||||
Text(0, "----------------------------^m^j");
|
||||
for Y:= 0 to 6 do
|
||||
[IntOut(0, Y); Text(0, " |");
|
||||
for X:= 0 to 5 do
|
||||
RlOut(0, float(F(N, X, Y)));
|
||||
CrLf(0);
|
||||
];
|
||||
CrLf(0);
|
||||
];
|
||||
Text(0, "F(2, 1, 1) = "); IntOut(0, F(2, 1, 1)); CrLf(0);
|
||||
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);
|
||||
]
|
||||
24
Task/Sudan-function/Yabasic/sudan-function.basic
Normal file
24
Task/Sudan-function/Yabasic/sudan-function.basic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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 F(n,x,y) using ("###");
|
||||
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
|
||||
|
||||
sub 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)
|
||||
end sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue