Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,6 @@
---
category:
- Memoization
- Classic CS problems and programs
from: http://rosettacode.org/wiki/Ackermann_function
note: Recursion

View file

@ -0,0 +1,28 @@
The '''[[wp:Ackermann function|Ackermann function]]''' is a classic example of a recursive function, notable especially because it is not a [[wp:Primitive_recursive_function|primitive recursive function]]. It grows very quickly in value, as does the size of its call tree.
The Ackermann function is usually defined as follows:
<big>
:<math> A(m, n) =
\begin{cases}
n+1 & \mbox{if } m = 0 \\
A(m-1, 1) & \mbox{if } m > 0 \mbox{ and } n = 0 \\
A(m-1, A(m, n-1)) & \mbox{if } m > 0 \mbox{ and } n > 0.
\end{cases}
</math>
</big>
<!-- <table><tr><td width=12><td><td><math>n+1</math><td>if <math>m=0</math> <tr><td> <td><math>A(m, n) =</math> <td><math>A(m-1, 1)</math> <td>if <math>m>0</math> and <math>n=0</math> <tr><td><td><td><math>A(m-1, A(m, n-1))</math>&nbsp;&nbsp;<td> if <math>m>0</math> and <math>n>0</math></table> -->
Its arguments are never negative and it always terminates.
;Task:
Write a function which returns the value of <math>A(m, n)</math>. Arbitrary precision is preferred (since the function grows so quickly), but not required.
;See also:
* [[wp:Conway_chained_arrow_notation#Ackermann_function|Conway chained arrow notation]] for the Ackermann function.
<br><br>

View file

@ -0,0 +1,11 @@
F ack2(m, n) -> Int
R I m == 0 {(n + 1)
} E I m == 1 {(n + 2)
} E I m == 2 {(2 * n + 3)
} E I m == 3 {(8 * (2 ^ n - 1) + 5)
} E I n == 0 {ack2(m - 1, 1)
} E ack2(m - 1, ack2(m, n - 1))
print(ack2(0, 0))
print(ack2(3, 4))
print(ack2(4, 1))

View file

@ -0,0 +1,99 @@
* Ackermann function 07/09/2015
&LAB XDECO &REG,&TARGET
.*-----------------------------------------------------------------*
.* THIS MACRO DISPLAYS THE REGISTER CONTENTS AS A TRUE *
.* DECIMAL VALUE. XDECO IS NOT PART OF STANDARD S360 MACROS! *
*------------------------------------------------------------------*
AIF (T'&REG EQ 'O').NOREG
AIF (T'&TARGET EQ 'O').NODEST
&LAB B I&SYSNDX BRANCH AROUND WORK AREA
W&SYSNDX DS XL8 CONVERSION WORK AREA
I&SYSNDX CVD &REG,W&SYSNDX CONVERT TO DECIMAL
MVC &TARGET,=XL12'402120202020202020202020'
ED &TARGET,W&SYSNDX+2 MAKE FIELD PRINTABLE
BC 2,*+12 BYPASS NEGATIVE
MVI &TARGET+12,C'-' INSERT NEGATIVE SIGN
B *+8 BYPASS POSITIVE
MVI &TARGET+12,C'+' INSERT POSITIVE SIGN
MEXIT
.NOREG MNOTE 8,'INPUT REGISTER OMITTED'
MEXIT
.NODEST MNOTE 8,'TARGET FIELD OMITTED'
MEXIT
MEND
ACKERMAN CSECT
USING ACKERMAN,R12 r12 : base register
LR R12,R15 establish base register
ST R14,SAVER14A save r14
LA R4,0 m=0
LOOPM CH R4,=H'3' do m=0 to 3
BH ELOOPM
LA R5,0 n=0
LOOPN CH R5,=H'8' do n=0 to 8
BH ELOOPN
LR R1,R4 m
LR R2,R5 n
BAL R14,ACKER r1=acker(m,n)
XDECO R1,PG+19
XDECO R4,XD
MVC PG+10(2),XD+10
XDECO R5,XD
MVC PG+13(2),XD+10
XPRNT PG,44 print buffer
LA R5,1(R5) n=n+1
B LOOPN
ELOOPN LA R4,1(R4) m=m+1
B LOOPM
ELOOPM L R14,SAVER14A restore r14
BR R14 return to caller
SAVER14A DS F static save r14
PG DC CL44'Ackermann(xx,xx) = xxxxxxxxxxxx'
XD DS CL12
ACKER CNOP 0,4 function r1=acker(r1,r2)
LR R3,R1 save argument r1 in r3
LR R9,R10 save stackptr (r10) in r9 temp
LA R1,STACKLEN amount of storage required
GETMAIN RU,LV=(R1) allocate storage for stack
USING STACK,R10 make storage addressable
LR R10,R1 establish stack addressability
ST R14,SAVER14B save previous r14
ST R9,SAVER10B save previous r10
LR R1,R3 restore saved argument r1
START ST R1,M stack m
ST R2,N stack n
IF1 C R1,=F'0' if m<>0
BNE IF2 then goto if2
LR R11,R2 n
LA R11,1(R11) return n+1
B EXIT
IF2 C R2,=F'0' else if m<>0
BNE IF3 then goto if3
BCTR R1,0 m=m-1
LA R2,1 n=1
BAL R14,ACKER r1=acker(m)
LR R11,R1 return acker(m-1,1)
B EXIT
IF3 BCTR R2,0 n=n-1
BAL R14,ACKER r1=acker(m,n-1)
LR R2,R1 acker(m,n-1)
L R1,M m
BCTR R1,0 m=m-1
BAL R14,ACKER r1=acker(m-1,acker(m,n-1))
LR R11,R1 return acker(m-1,1)
EXIT L R14,SAVER14B restore r14
L R9,SAVER10B restore r10 temp
LA R0,STACKLEN amount of storage to free
FREEMAIN A=(R10),LV=(R0) free allocated storage
LR R1,R11 value returned
LR R10,R9 restore r10
BR R14 return to caller
LTORG
DROP R12 base no longer needed
STACK DSECT dynamic area
SAVER14B DS F saved r14
SAVER10B DS F saved r10
M DS F m
N DS F n
STACKLEN EQU *-STACK
YREGS
END ACKERMAN

View file

@ -0,0 +1,112 @@
;
; Ackermann function for Motorola 68000 under AmigaOs 2+ by Thorham
;
; Set stack space to 60000 for m = 3, n = 5.
;
; The program will print the ackermann values for the range m = 0..3, n = 0..5
;
_LVOOpenLibrary equ -552
_LVOCloseLibrary equ -414
_LVOVPrintf equ -954
m equ 3 ; Nr of iterations for the main loop.
n equ 5 ; Do NOT set them higher, or it will take hours to complete on
; 68k, not to mention that the stack usage will become astronomical.
; Perhaps n can be a little higher... If you do increase the ranges
; then don't forget to increase the stack size.
execBase=4
start
move.l execBase,a6
lea dosName,a1
moveq #36,d0
jsr _LVOOpenLibrary(a6)
move.l d0,dosBase
beq exit
move.l dosBase,a6
lea printfArgs,a2
clr.l d3 ; m
.loopn
clr.l d4 ; n
.loopm
bsr ackermann
move.l d3,0(a2)
move.l d4,4(a2)
move.l d5,8(a2)
move.l #outString,d1
move.l a2,d2
jsr _LVOVPrintf(a6)
addq.l #1,d4
cmp.l #n,d4
ble .loopm
addq.l #1,d3
cmp.l #m,d3
ble .loopn
exit
move.l execBase,a6
move.l dosBase,a1
jsr _LVOCloseLibrary(a6)
rts
;
; ackermann function
;
; in:
;
; d3 = m
; d4 = n
;
; out:
;
; d5 = ans
;
ackermann
move.l d3,-(sp)
move.l d4,-(sp)
tst.l d3
bne .l1
move.l d4,d5
addq.l #1,d5
bra .return
.l1
tst.l d4
bne .l2
subq.l #1,d3
moveq #1,d4
bsr ackermann
bra .return
.l2
subq.l #1,d4
bsr ackermann
move.l d5,d4
subq.l #1,d3
bsr ackermann
.return
move.l (sp)+,d4
move.l (sp)+,d3
rts
;
; variables
;
dosBase
dc.l 0
printfArgs
dcb.l 3
;
; strings
;
dosName
dc.b "dos.library",0
outString
dc.b "ackermann (%ld,%ld) is: %ld",10,0

View file

@ -0,0 +1,81 @@
org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ACK(M,N); DE=M, HL=N, return value in HL.
ack: mov a,d ; M=0?
ora e
jnz ackm
inx h ; If so, N+1.
ret
ackm: mov a,h ; N=0?
ora l
jnz ackmn
lxi h,1 ; If so, N=1,
dcx d ; N-=1,
jmp ack ; A(M,N) - tail recursion
ackmn: push d ; M>0 and N>0: store M on the stack
dcx h ; N-=1
call ack ; N = ACK(M,N-1)
pop d ; Restore previous M
dcx d ; M-=1
jmp ack ; A(M,N) - tail recursion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Print table of ack(m,n)
MMAX: equ 4 ; Size of table to print. Note that math is done in
NMAX: equ 9 ; 16 bits.
demo: lhld 6 ; Put stack pointer at top of available memory
sphl
lxi b,0 ; let B,C hold 8-bit M and N.
acknum: xra a ; Set high bit of M and N to zero
mov d,a ; DE = B (M)
mov e,b
mov h,a ; HL = C (N)
mov l,c
call ack ; HL = ack(DE,HL)
call prhl ; Print the number
inr c ; N += 1
mvi a,NMAX ; Time for next line?
cmp c
jnz acknum ; If not, print next number
push b ; Otherwise, save BC
mvi c,9 ; Print newline
lxi d,nl
call 5
pop b ; Restore BC
mvi c,0 ; Set N to 0
inr b ; M += 1
mvi a,MMAX ; Time to stop?
cmp b
jnz acknum ; If not, print next number
rst 0
;;; Print HL as ASCII number.
prhl: push h ; Save all registers
push d
push b
lxi b,pnum ; Store pointer to num string on stack
push b
lxi b,-10 ; Divisor
prdgt: lxi d,-1
prdgtl: inx d ; Divide by 10 through trial subtraction
dad b
jc prdgtl
mvi a,'0'+10
add l ; L = remainder - 10
pop h ; Get pointer from stack
dcx h ; Store digit
mov m,a
push h ; Put pointer back on stack
xchg ; Put quotient in HL
mov a,h ; Check if zero
ora l
jnz prdgt ; If not, next digit
pop d ; Get pointer and put in DE
mvi c,9 ; CP/M print string
call 5
pop b ; Restore registers
pop d
pop h
ret
db '*****' ; Placeholder for number
pnum: db 9,'$'
nl: db 13,10,'$'

View file

@ -0,0 +1,60 @@
cpu 8086
bits 16
org 100h
section .text
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ACK(M,N); DX=M, AX=N, return value in AX.
ack: and dx,dx ; N=0?
jnz .m
inc ax ; If so, return N+1
ret
.m: and ax,ax ; M=0?
jnz .mn
mov ax,1 ; If so, N=1,
dec dx ; M -= 1
jmp ack ; ACK(M-1,1) - tail recursion
.mn: push dx ; Keep M on the stack
dec ax ; N-=1
call ack ; N = ACK(M,N-1)
pop dx ; Restore M
dec dx ; M -= 1
jmp ack ; ACK(M-1,ACK(M,N-1)) - tail recursion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Print table of ack(m,n)
MMAX: equ 4 ; Size of table to print. Noe that math is done
NMAX: equ 9 ; in 16 bits.
demo: xor si,si ; Let SI hold M,
xor di,di ; and DI hold N.
acknum: mov dx,si ; Calculate ack(M,N)
mov ax,di
call ack
call prax ; Print number
inc di ; N += 1
cmp di,NMAX ; Row done?
jb acknum ; If not, print next number on row
xor di,di ; Otherwise, N=0,
inc si ; M += 1
mov dx,nl ; Print newline
call prstr
cmp si,MMAX ; Done?
jb acknum ; If not, start next row
ret ; Otherwise, stop.
;;; Print AX as ASCII number.
prax: mov bx,pnum ; Pointer to number string
mov cx,10 ; Divisor
.dgt: xor dx,dx ; Divide AX by ten
div cx
add dl,'0' ; DX holds remainder - add ASCII 0
dec bx ; Move pointer backwards
mov [bx],dl ; Save digit in string
and ax,ax ; Are we done yet?
jnz .dgt ; If not, next digit
mov dx,bx ; Tell DOS to print the string
prstr: mov ah,9
int 21h
ret
section .data
db '*****' ; Placeholder for ASCII number
pnum: db 9,'$'
nl: db 13,10,'$'

View file

@ -0,0 +1,75 @@
\ Ackermann function, illustrating use of "memoization".
\ Memoization is a technique whereby intermediate computed values are stored
\ away against later need. It is particularly valuable when calculating those
\ values is time or resource intensive, as with the Ackermann function.
\ make the stack much bigger so this can complete!
100000 stack-size
\ This is where memoized values are stored:
{} var, dict
\ Simple accessor words
: dict! \ "key" val --
dict @ -rot m:! drop ;
: dict@ \ "key" -- val
dict @ swap m:@ nip ;
defer: ack1
\ We just jam the string representation of the two numbers together for a key:
: makeKey \ m n -- m n key
2dup >s swap >s s:+ ;
: ack2 \ m n -- A
makeKey dup
dict@ null?
if \ can't find key in dict
\ m n key null
drop \ m n key
-rot \ key m n
ack1 \ key A
tuck \ A key A
dict! \ A
else \ found value
\ m n key value
>r drop 2drop r>
then ;
: ack \ m n -- A
over not
if
nip n:1+
else
dup not
if
drop n:1- 1 ack2
else
over swap n:1- ack2
swap n:1- swap ack2
then
then ;
' ack is ack1
: ackOf \ m n --
2dup
"Ack(" . swap . ", " . . ") = " . ack . cr ;
0 0 ackOf
0 4 ackOf
1 0 ackOf
1 1 ackOf
2 1 ackOf
2 2 ackOf
3 1 ackOf
3 3 ackOf
4 0 ackOf
\ this last requires a very large data stack. So start 8th with a parameter '-k 100000'
4 1 ackOf
bye

View file

@ -0,0 +1,121 @@
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program ackermann64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ MMAXI, 4
.equ NMAXI, 10
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "Result for @ @ : @ \n"
szMessError: .asciz "Overflow !!.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x3,#0
mov x4,#0
1:
mov x0,x3
mov x1,x4
bl ackermann
mov x5,x0
mov x0,x3
ldr x1,qAdrsZoneConv // else display odd message
bl conversion10 // call decimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv // insert value conversion in message
bl strInsertAtCharInc
mov x6,x0
mov x0,x4
ldr x1,qAdrsZoneConv // else display odd message
bl conversion10 // call decimal conversion
mov x0,x6
ldr x1,qAdrsZoneConv // insert value conversion in message
bl strInsertAtCharInc
mov x6,x0
mov x0,x5
ldr x1,qAdrsZoneConv // else display odd message
bl conversion10 // call decimal conversion
mov x0,x6
ldr x1,qAdrsZoneConv // insert value conversion in message
bl strInsertAtCharInc
bl affichageMess
add x4,x4,#1
cmp x4,#NMAXI
blt 1b
mov x4,#0
add x3,x3,#1
cmp x3,#MMAXI
blt 1b
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrsZoneConv: .quad sZoneConv
/***************************************************/
/* fonction ackermann */
/***************************************************/
// x0 contains a number m
// x1 contains a number n
// x0 return résult
ackermann:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
cmp x0,0
beq 5f
mov x3,-1
csel x0,x3,x0,lt // error
blt 100f
cmp x1,#0
csel x0,x3,x0,lt // error
blt 100f
bgt 1f
sub x0,x0,#1
mov x1,#1
bl ackermann
b 100f
1:
mov x2,x0
sub x1,x1,#1
bl ackermann
mov x1,x0
sub x0,x2,#1
bl ackermann
b 100f
5:
adds x0,x1,#1
bcc 100f
ldr x0,qAdrszMessError
bl affichageMess
mov x0,-1
100:
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x1,lr,[sp],16 // restaur des 2 registres
ret
qAdrszMessError: .quad szMessError
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,45 @@
REPORT zhuberv_ackermann.
CLASS zcl_ackermann DEFINITION.
PUBLIC SECTION.
CLASS-METHODS ackermann IMPORTING m TYPE i
n TYPE i
RETURNING value(v) TYPE i.
ENDCLASS. "zcl_ackermann DEFINITION
CLASS zcl_ackermann IMPLEMENTATION.
METHOD: ackermann.
DATA: lv_new_m TYPE i,
lv_new_n TYPE i.
IF m = 0.
v = n + 1.
ELSEIF m > 0 AND n = 0.
lv_new_m = m - 1.
lv_new_n = 1.
v = ackermann( m = lv_new_m n = lv_new_n ).
ELSEIF m > 0 AND n > 0.
lv_new_m = m - 1.
lv_new_n = n - 1.
lv_new_n = ackermann( m = m n = lv_new_n ).
v = ackermann( m = lv_new_m n = lv_new_n ).
ENDIF.
ENDMETHOD. "ackermann
ENDCLASS. "zcl_ackermann IMPLEMENTATION
PARAMETERS: pa_m TYPE i,
pa_n TYPE i.
DATA: lv_result TYPE i.
START-OF-SELECTION.
lv_result = zcl_ackermann=>ackermann( m = pa_m n = pa_n ).
WRITE: / lv_result.

View file

@ -0,0 +1,12 @@
begin
integer procedure ackermann(m,n);value m,n;integer m,n;
ackermann:=if m=0 then n+1
else if n=0 then ackermann(m-1,1)
else ackermann(m-1,ackermann(m,n-1));
integer m,n;
for m:=0 step 1 until 3 do begin
for n:=0 step 1 until 6 do
outinteger(1,ackermann(m,n));
outstring(1,"\n")
end
end

View file

@ -0,0 +1,21 @@
PROC test ackermann = VOID:
BEGIN
PROC ackermann = (INT m, n)INT:
BEGIN
IF m = 0 THEN
n + 1
ELIF n = 0 THEN
ackermann (m - 1, 1)
ELSE
ackermann (m - 1, ackermann (m, n - 1))
FI
END # ackermann #;
FOR m FROM 0 TO 3 DO
FOR n FROM 0 TO 6 DO
print(ackermann (m, n))
OD;
new line(stand out)
OD
END # test ackermann #;
test ackermann

View file

@ -0,0 +1,10 @@
begin
integer procedure ackermann( integer value m,n ) ;
if m=0 then n+1
else if n=0 then ackermann(m-1,1)
else ackermann(m-1,ackermann(m,n-1));
for m := 0 until 3 do begin
write( ackermann( m, 0 ) );
for n := 1 until 6 do writeon( ackermann( m, n ) );
end for_m
end.

View file

@ -0,0 +1,5 @@
ackermann{
0=1:1+2
0=2:(¯1+1)1
(¯1+1),(1),¯1+2
}

View file

@ -0,0 +1,119 @@
/* ARM assembly Raspberry PI or android 32 bits */
/* program ackermann.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ MMAXI, 4
.equ NMAXI, 10
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "Result for @ @ : @ \n"
szMessError: .asciz "Overflow !!.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
mov r3,#0
mov r4,#0
1:
mov r0,r3
mov r1,r4
bl ackermann
mov r5,r0
mov r0,r3
ldr r1,iAdrsZoneConv @ else display odd message
bl conversion10 @ call decimal conversion
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert value conversion in message
bl strInsertAtCharInc
mov r6,r0
mov r0,r4
ldr r1,iAdrsZoneConv @ else display odd message
bl conversion10 @ call decimal conversion
mov r0,r6
ldr r1,iAdrsZoneConv @ insert value conversion in message
bl strInsertAtCharInc
mov r6,r0
mov r0,r5
ldr r1,iAdrsZoneConv @ else display odd message
bl conversion10 @ call decimal conversion
mov r0,r6
ldr r1,iAdrsZoneConv @ insert value conversion in message
bl strInsertAtCharInc
bl affichageMess
add r4,#1
cmp r4,#NMAXI
blt 1b
mov r4,#0
add r3,#1
cmp r3,#MMAXI
blt 1b
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* fonction ackermann */
/***************************************************/
// r0 contains a number m
// r1 contains a number n
// r0 return résult
ackermann:
push {r1-r2,lr} @ save registers
cmp r0,#0
beq 5f
movlt r0,#-1 @ error
blt 100f
cmp r1,#0
movlt r0,#-1 @ error
blt 100f
bgt 1f
sub r0,r0,#1
mov r1,#1
bl ackermann
b 100f
1:
mov r2,r0
sub r1,r1,#1
bl ackermann
mov r1,r0
sub r0,r2,#1
bl ackermann
b 100f
5:
adds r0,r1,#1
bcc 100f
ldr r0,iAdrszMessError
bl affichageMess
bkpt
100:
pop {r1-r2,lr} @ restaur registers
bx lr @ return
iAdrszMessError: .int szMessError
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"

View file

@ -0,0 +1,8 @@
fun ackermann
{m,n:nat} .<m,n>.
(m: int m, n: int n): Nat =
case+ (m, n) of
| (0, _) => n+1
| (_, 0) =>> ackermann (m-1, 1)
| (_, _) =>> ackermann (m-1, ackermann (m, n-1))
// end of [ackermann]

View file

@ -0,0 +1,18 @@
function ackermann(m, n)
{
if ( m == 0 ) {
return n+1
}
if ( n == 0 ) {
return ackermann(m-1, 1)
}
return ackermann(m-1, ackermann(m, n-1))
}
BEGIN {
for(n=0; n < 7; n++) {
for(m=0; m < 4; m++) {
print "A(" m "," n ") = " ackermann(m,n)
}
}
}

View file

@ -0,0 +1,57 @@
DEFINE MAXSIZE="1000"
CARD ARRAY stack(MAXSIZE)
CARD stacksize=[0]
BYTE FUNC IsEmpty()
IF stacksize=0 THEN
RETURN (1)
FI
RETURN (0)
PROC Push(BYTE v)
IF stacksize=maxsize THEN
PrintE("Error: stack is full!")
Break()
FI
stack(stacksize)=v
stacksize==+1
RETURN
BYTE FUNC Pop()
IF IsEmpty() THEN
PrintE("Error: stack is empty!")
Break()
FI
stacksize==-1
RETURN (stack(stacksize))
CARD FUNC Ackermann(CARD m,n)
Push(m)
WHILE IsEmpty()=0
DO
m=Pop()
IF m=0 THEN
n==+1
ELSEIF n=0 THEN
n=1
Push(m-1)
ELSE
n==-1
Push(m-1)
Push(m)
FI
OD
RETURN (n)
PROC Main()
CARD m,n,res
FOR m=0 TO 3
DO
FOR n=0 TO 4
DO
res=Ackermann(m,n)
PrintF("Ack(%U,%U)=%U%E",m,n,res)
OD
OD
RETURN

View file

@ -0,0 +1,13 @@
public function ackermann(m:uint, n:uint):uint
{
if (m == 0)
{
return n + 1;
}
if (n == 0)
{
return ackermann(m - 1, 1);
}
return ackermann(m - 1, ackermann(m, n - 1));
}

View file

@ -0,0 +1,21 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Ackermann is
function Ackermann (M, N : Natural) return Natural is
begin
if M = 0 then
return N + 1;
elsif N = 0 then
return Ackermann (M - 1, 1);
else
return Ackermann (M - 1, Ackermann (M, N - 1));
end if;
end Ackermann;
begin
for M in 0..3 loop
for N in 0..6 loop
Put (Natural'Image (Ackermann (M, N)));
end loop;
New_Line;
end loop;
end Test_Ackermann;

View file

@ -0,0 +1,25 @@
module Ackermann where
open import Data.Nat using ( ; zero ; suc ; _+_)
ack :
ack zero n = n + 1
ack (suc m) zero = ack m 1
ack (suc m) (suc n) = ack m (ack (suc m) n)
open import Agda.Builtin.IO using (IO)
open import Agda.Builtin.Unit using ()
open import Agda.Builtin.String using (String)
open import Data.Nat.Show using (show)
postulate putStrLn : String IO
{-# FOREIGN GHC import qualified Data.Text as T #-}
{-# COMPILE GHC putStrLn = putStrLn . T.unpack #-}
main : IO
main = putStrLn (show (ack 3 9))
-- Output:
-- 4093

View file

@ -0,0 +1,2 @@
agda --compile Ackermann.agda
./Ackermann

View file

@ -0,0 +1,5 @@
on ackermann(m, n)
if m is equal to 0 then return n + 1
if n is equal to 0 then return ackermann(m - 1, 1)
return ackermann(m - 1, ackermann(m, n - 1))
end ackermann

View file

@ -0,0 +1,24 @@
100 DIM R%(2900),M(2900),N(2900)
110 FOR M = 0 TO 3
120 FOR N = 0 TO 4
130 GOSUB 200"ACKERMANN
140 PRINT "ACK("M","N") = "ACK
150 NEXT N, M
160 END
200 M(SP) = M
210 N(SP) = N
REM A(M - 1, A(M, N - 1))
220 IF M > 0 AND N > 0 THEN N = N - 1 : R%(SP) = 0 : SP = SP + 1 : GOTO 200
REM A(M - 1, 1)
230 IF M > 0 THEN M = M - 1 : N = 1 : R%(SP) = 1 : SP = SP + 1 : GOTO 200
REM N + 1
240 ACK = N + 1
REM RETURN
250 M = M(SP) : N = N(SP) : IF SP = 0 THEN RETURN
260 FOR SP = SP - 1 TO 0 STEP -1 : IF R%(SP) THEN M = M(SP) : N = N(SP) : NEXT SP : SP = 0 : RETURN
270 M = M - 1 : N = ACK : R%(SP) = 1 : SP = SP + 1 : GOTO 200

View file

@ -0,0 +1,10 @@
use std
for each (val nat n) from 0 to 6
for each (val nat m) from 0 to 3
print "A("m","n") = "(A m n)
.:A <nat m, nat n>:. -> nat
return (n+1) if m == 0
return (A (m - 1) 1) if n == 0
A (m - 1) (A m (n - 1))

View file

@ -0,0 +1,12 @@
ackermann: function [m,n][
(m=0)? -> n+1 [
(n=0)? -> ackermann m-1 1
-> ackermann m-1 ackermann m n-1
]
]
loop 0..3 'a [
loop 0..4 'b [
print ["ackermann" a b "=>" ackermann a b]
]
]

View file

@ -0,0 +1,11 @@
A(m, n) {
If (m > 0) && (n = 0)
Return A(m-1,1)
Else If (m > 0) && (n > 0)
Return A(m-1,A(m, n-1))
Else If (m=0)
Return n+1
}
; Example:
MsgBox, % "A(1,2) = " A(1,2)

View file

@ -0,0 +1,11 @@
Func Ackermann($m, $n)
If ($m = 0) Then
Return $n+1
Else
If ($n = 0) Then
Return Ackermann($m-1, 1)
Else
return Ackermann($m-1, Ackermann($m, $n-1))
EndIf
EndIf
EndFunc

View file

@ -0,0 +1,18 @@
Global $ackermann[2047][2047] ; Set the size to whatever you want
Func Ackermann($m, $n)
If ($ackermann[$m][$n] <> 0) Then
Return $ackermann[$m][$n]
Else
If ($m = 0) Then
$return = $n + 1
Else
If ($n = 0) Then
$return = Ackermann($m - 1, 1)
Else
$return = Ackermann($m - 1, Ackermann($m, $n - 1))
EndIf
EndIf
$ackermann[$m][$n] = $return
Return $return
EndIf
EndFunc ;==>Ackermann

View file

@ -0,0 +1,33 @@
dim stack(5000, 3) # BASIC-256 lacks functions (as of ver. 0.9.6.66)
stack[0,0] = 3 # M
stack[0,1] = 7 # N
lev = 0
gosub ackermann
print "A("+stack[0,0]+","+stack[0,1]+") = "+stack[0,2]
end
ackermann:
if stack[lev,0]=0 then
stack[lev,2] = stack[lev,1]+1
return
end if
if stack[lev,1]=0 then
lev = lev+1
stack[lev,0] = stack[lev-1,0]-1
stack[lev,1] = 1
gosub ackermann
stack[lev-1,2] = stack[lev,2]
lev = lev-1
return
end if
lev = lev+1
stack[lev,0] = stack[lev-1,0]
stack[lev,1] = stack[lev-1,1]-1
gosub ackermann
stack[lev,0] = stack[lev-1,0]-1
stack[lev,1] = stack[lev,2]
gosub ackermann
stack[lev-1,2] = stack[lev,2]
lev = lev-1
return

View file

@ -0,0 +1,19 @@
# BASIC256 since 0.9.9.1 supports functions
for m = 0 to 3
for n = 0 to 4
print m + " " + n + " " + ackermann(m,n)
next n
next m
end
function ackermann(m,n)
if m = 0 then
ackermann = n+1
else
if n = 0 then
ackermann = ackermann(m-1,1)
else
ackermann = ackermann(m-1,ackermann(m,n-1))
endif
end if
end function

View file

@ -0,0 +1,7 @@
PRINT FNackermann(3, 7)
END
DEF FNackermann(M%, N%)
IF M% = 0 THEN = N% + 1
IF N% = 0 THEN = FNackermann(M% - 1, 1)
= FNackermann(M% - 1, FNackermann(M%, N%-1))

View file

@ -0,0 +1,11 @@
GET "libhdr"
LET ack(m, n) = m=0 -> n+1,
n=0 -> ack(m-1, 1),
ack(m-1, ack(m, n-1))
LET start() = VALOF
{ FOR i = 0 TO 6 FOR m = 0 TO 3 DO
writef("ack(%n, %n) = %n*n", m, n, ack(m,n))
RESULTIS 0
}

View file

@ -0,0 +1,5 @@
A {
A 0n: n+1;
A m0: A (m-1)1;
A mn: A (m-1)(A m(n-1))
}

View file

@ -0,0 +1,8 @@
A 03
4
A 14
6
A 24
11
A 34
125

View file

@ -0,0 +1,32 @@
main:
{((0 0) (0 1) (0 2)
(0 3) (0 4) (1 0)
(1 1) (1 2) (1 3)
(1 4) (2 0) (2 1)
(2 2) (2 3) (3 0)
(3 1) (3 2) (4 0))
{ dup
"A(" << { %d " " . << } ... ") = " <<
reverse give
ack
%d cr << } ... }
ack!:
{ dup zero?
{ <-> dup zero?
{ <->
cp
1 -
<- <- 1 - ->
ack ->
ack }
{ <->
1 -
<- 1 ->
ack }
if }
{ zap 1 + }
if }
zero?!: { 0 = }

View file

@ -0,0 +1,31 @@
::Ackermann.cmd
@echo off
set depth=0
:ack
if %1==0 goto m0
if %2==0 goto n0
:else
set /a n=%2-1
set /a depth+=1
call :ack %1 %n%
set t=%errorlevel%
set /a depth-=1
set /a m=%1-1
set /a depth+=1
call :ack %m% %t%
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )
:m0
set/a n=%2+1
if %depth%==0 ( exit %n% ) else ( exit /b %n% )
:n0
set /a m=%1-1
set /a depth+=1
call :ack %m% 1
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

View file

@ -0,0 +1,4 @@
::Ack.cmd
@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann(%1, %2)=%errorlevel%

View file

@ -0,0 +1,12 @@
define ack(m, n) {
if ( m == 0 ) return (n+1);
if ( n == 0 ) return (ack(m-1, 1));
return (ack(m-1, ack(m, n-1)));
}
for (n=0; n<7; n++) {
for (m=0; m<4; m++) {
print "A(", m, ",", n, ") = ", ack(m,n), "\n";
}
}
quit

View file

@ -0,0 +1,5 @@
>M?f@h@gMf@h3yzp if m>0 and n>0 => replace m,n with m-1,m,n-1
>@h@g'b?1f@h@gM?f@hzp if m>0 and n=0 => replace m,n with m-1,1
_ii>Ag~1?~Lpz1~2h@g'd?g?Pfzp if m=0 => replace m,n with n+1
>I;
b < <

View file

@ -0,0 +1,4 @@
Ag~1?Lp1~2@g'p?g?Pf1FJ Ackermann function. if m=0 => run Ackermann function (m, n+1)
xI; x@g'p??@Mf1fFJ if m>0 and n=0 => run Ackermann (m-1,1)
xM@~gM??f~f@f1FJ if m>0 and n>0 => run Ackermann(m,Ackermann(m-1,n-1))
_ii1FJ input function. Input m,n, then execute Ackermann(m,n)

View file

@ -0,0 +1,10 @@
>Mf@Ph?@g@2h@Mf@Php if m>4 and n>0 => replace m,n with m-1,m,n-1
>~4~L#1~2hg'd?1f@hgM?f2h p if m>4 and n=0 => replace m,n with m-1,1
# q < /n+3 times \
#X~4K#?2Fg?PPP>@B@M"pb if m=4 => replace m,n with 2^(2^(....)))-3
# >~3K#?g?PPP~2BMMp>@MMMp if m=3 => replace m,n with 2^(n+3)-3
_ii>Ag~1?~Lpz1~2h@gX'#?g?P p M if m=0 => replace m,n with n+1
z I ~>~1K#?g?PP p if m=1 => replace m,n with n+2
f ; >2K#?g?~2.PPPp if m=2 => replace m,n with 2n+3
z b < < <
d <

View file

@ -0,0 +1,3 @@
&>&>vvg0>#0\#-:#1_1v
@v:\<vp0 0:-1<\+<
^>00p>:#^_$1+\:#^_$.

View file

@ -0,0 +1,8 @@
r[1&&{0
>v
j
u>.@
1> \:v
^ v:\_$1+
\^v_$1\1-
u^>1-0fp:1-\0fg101-

View file

@ -0,0 +1,8 @@
( Ack
= m n
. !arg:(?m,?n)
& ( !m:0&!n+1
| !n:0&Ack$(!m+-1,1)
| Ack$(!m+-1,Ack$(!m,!n+-1))
)
);

View file

@ -0,0 +1,61 @@
( A
= m n value key eq chain
, find insert future stack va val
. ( chain
= key future skey
. !arg:(?key.?future)
& str$!key:?skey
& (cache..insert)$(!skey..!future)
&
)
& (find=.(cache..find)$(str$!arg))
& ( insert
= key value future v futureeq futurem skey
. !arg:(?key.?value)
& str$!key:?skey
& ( (cache..find)$!skey:(?key.?v.?future)
& (cache..remove)$!skey
& (cache..insert)$(!skey.!value.)
& ( !future:(?futurem.?futureeq)
& (!futurem,!value.!futureeq)
|
)
| (cache..insert)$(!skey.!value.)&
)
)
& !arg:(?m,?n)
& !n+1:?value
& :?eq:?stack
& whl
' ( (!m,!n):?key
& ( find$!key:(?.#%?value.?future)
& insert$(!eq.!value) !future
| !m:0
& !n+1:?value
& ( !eq:&insert$(!key.!value)
| insert$(!key.!value) !stack:?stack
& insert$(!eq.!value)
)
| !n:0
& (!m+-1,1.!key)
(!eq:|(!key.!eq))
| find$(!m,!n+-1):(?.?val.?)
& ( !val:#%
& ( find$(!m+-1,!val):(?.?va.?)
& !va:#%
& insert$(!key.!va)
| (!m+-1,!val.!eq)
(!m,!n.!eq)
)
|
)
| chain$(!m,!n+-1.!m+-1.!key)
& (!m,!n+-1.)
(!eq:|(!key.!eq))
)
!stack
: (?m,?n.?eq) ?stack
)
& !value
)
& new$hash:?cache

View file

@ -0,0 +1,11 @@
( AckFormula
= m n
. !arg:(?m,?n)
& ( !m:0&!n+1
| !m:1&!n+2
| !m:2&2*!n+3
| !m:3&2^(!n+3)+-3
| !n:0&AckFormula$(!m+-1,1)
| AckFormula$(!m+-1,AckFormula$(!m,!n+-1))
)
)

View file

@ -0,0 +1,7 @@
ackermann = { m, n |
when { m == 0 } { n + 1 }
{ m > 0 && n == 0 } { ackermann(m - 1, 1) }
{ m > 0 && n > 0 } { ackermann(m - 1, ackermann(m, n - 1)) }
}
p ackermann 3, 4 #Prints 125

View file

@ -0,0 +1,19 @@
#include <iostream>
unsigned int ackermann(unsigned int m, unsigned int n) {
if (m == 0) {
return n + 1;
}
if (n == 0) {
return ackermann(m - 1, 1);
}
return ackermann(m - 1, ackermann(m, n - 1));
}
int main() {
for (unsigned int m = 0; m < 4; ++m) {
for (unsigned int n = 0; n < 10; ++n) {
std::cout << "A(" << m << ", " << n << ") = " << ackermann(m, n) << "\n";
}
}
}

View file

@ -0,0 +1,54 @@
#include <iostream>
#include <sstream>
#include <string>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost::multiprecision::cpp_int;
big_int ipow(big_int base, big_int exp) {
big_int result(1);
while (exp) {
if (exp & 1) {
result *= base;
}
exp >>= 1;
base *= base;
}
return result;
}
big_int ackermann(unsigned m, unsigned n) {
static big_int (*ack)(unsigned, big_int) =
[](unsigned m, big_int n)->big_int {
switch (m) {
case 0:
return n + 1;
case 1:
return n + 2;
case 2:
return 3 + 2 * n;
case 3:
return 5 + 8 * (ipow(big_int(2), n) - 1);
default:
return n == 0 ? ack(m - 1, big_int(1)) : ack(m - 1, ack(m, n - 1));
}
};
return ack(m, big_int(n));
}
int main() {
for (unsigned m = 0; m < 4; ++m) {
for (unsigned n = 0; n < 10; ++n) {
std::cout << "A(" << m << ", " << n << ") = " << ackermann(m, n) << "\n";
}
}
std::cout << "A(4, 1) = " << ackermann(4, 1) << "\n";
std::stringstream ss;
ss << ackermann(4, 2);
auto text = ss.str();
std::cout << "A(4, 2) = (" << text.length() << " digits)\n"
<< text.substr(0, 80) << "\n...\n"
<< text.substr(text.length() - 80) << "\n";
}

View file

@ -0,0 +1,32 @@
using System;
class Program
{
public static long Ackermann(long m, long n)
{
if(m > 0)
{
if (n > 0)
return Ackermann(m - 1, Ackermann(m, n - 1));
else if (n == 0)
return Ackermann(m - 1, 1);
}
else if(m == 0)
{
if(n >= 0)
return n + 1;
}
throw new System.ArgumentOutOfRangeException();
}
static void Main()
{
for (long m = 0; m <= 3; ++m)
{
for (long n = 0; n <= 4; ++n)
{
Console.WriteLine("Ackermann({0}, {1}) = {2}", m, n, Ackermann(m, n));
}
}
}
}

View file

@ -0,0 +1,127 @@
using System;
using System.Numerics;
using System.IO;
using System.Diagnostics;
namespace Ackermann_Function
{
class Program
{
static void Main(string[] args)
{
int _m = 0;
int _n = 0;
Console.Write("m = ");
try
{
_m = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Please enter a number.");
}
Console.Write("n = ");
try
{
_n = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Please enter a number.");
}
//for (long m = 0; m <= 10; ++m)
//{
// for (long n = 0; n <= 10; ++n)
// {
// DateTime now = DateTime.Now;
// Console.WriteLine("Ackermann({0}, {1}) = {2}", m, n, Ackermann(m, n));
// Console.WriteLine("Time taken:{0}", DateTime.Now - now);
// }
//}
DateTime now = DateTime.Now;
Console.WriteLine("Ackermann({0}, {1}) = {2}", _m, _n, Ackermann(_m, _n));
Console.WriteLine("Time taken:{0}", DateTime.Now - now);
File.WriteAllText("number.txt", Ackermann(_m, _n).ToString());
Process.Start("number.txt");
Console.ReadKey();
}
public class OverflowlessStack<T>
{
internal sealed class SinglyLinkedNode
{
private const int ArraySize = 2048;
T[] _array;
int _size;
public SinglyLinkedNode Next;
public SinglyLinkedNode()
{
_array = new T[ArraySize];
}
public bool IsEmpty { get { return _size == 0; } }
public SinglyLinkedNode Push(T item)
{
if (_size == ArraySize - 1)
{
SinglyLinkedNode n = new SinglyLinkedNode();
n.Next = this;
n.Push(item);
return n;
}
_array[_size++] = item;
return this;
}
public T Pop()
{
return _array[--_size];
}
}
private SinglyLinkedNode _head = new SinglyLinkedNode();
public T Pop()
{
T ret = _head.Pop();
if (_head.IsEmpty && _head.Next != null)
_head = _head.Next;
return ret;
}
public void Push(T item)
{
_head = _head.Push(item);
}
public bool IsEmpty
{
get { return _head.Next == null && _head.IsEmpty; }
}
}
public static BigInteger Ackermann(BigInteger m, BigInteger n)
{
var stack = new OverflowlessStack<BigInteger>();
stack.Push(m);
while (!stack.IsEmpty)
{
m = stack.Pop();
skipStack:
if (m == 0)
n = n + 1;
else if (m == 1)
n = n + 2;
else if (m == 2)
n = n * 2 + 3;
else if (n == 0)
{
--m;
n = 1;
goto skipStack;
}
else
{
stack.Push(m - 1);
--n;
goto skipStack;
}
}
return n;
}
}
}

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int ackermann(int m, int n)
{
if (!m) return n + 1;
if (!n) return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}
int main()
{
int m, n;
for (m = 0; m <= 4; m++)
for (n = 0; n < 6 - m; n++)
printf("A(%d, %d) = %d\n", m, n, ackermann(m, n));
return 0;
}

View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int m_bits, n_bits;
int *cache;
int ackermann(int m, int n)
{
int idx, res;
if (!m) return n + 1;
if (n >= 1<<n_bits) {
printf("%d, %d\n", m, n);
idx = 0;
} else {
idx = (m << n_bits) + n;
if (cache[idx]) return cache[idx];
}
if (!n) res = ackermann(m - 1, 1);
else res = ackermann(m - 1, ackermann(m, n - 1));
if (idx) cache[idx] = res;
return res;
}
int main()
{
int m, n;
m_bits = 3;
n_bits = 20; /* can save n values up to 2**20 - 1, that's 1 meg */
cache = malloc(sizeof(int) * (1 << (m_bits + n_bits)));
memset(cache, 0, sizeof(int) * (1 << (m_bits + n_bits)));
for (m = 0; m <= 4; m++)
for (n = 0; n < 6 - m; n++)
printf("A(%d, %d) = %d\n", m, n, ackermann(m, n));
return 0;
}

View file

@ -0,0 +1,100 @@
/* Thejaka Maldeniya */
#include <conio.h>
unsigned long long HR(unsigned int n, unsigned long long a, unsigned long long b) {
// (Internal) Recursive Hyperfunction: Perform a Hyperoperation...
unsigned long long r = 1;
while(b--)
r = n - 3 ? HR(n - 1, a, r) : /* Exponentiation */ r * a;
return r;
}
unsigned long long H(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Recursive-Iterative-O(1) Hybrid): Perform a Hyperoperation...
switch(n) {
case 0:
// Increment
return ++b;
case 1:
// Addition
return a + b;
case 2:
// Multiplication
return a * b;
}
return HR(n, a, b);
}
unsigned long long APH(unsigned int m, unsigned int n) {
// Ackermann-Péter Function (Recursive-Iterative-O(1) Hybrid)
return H(m, 2, n + 3) - 3;
}
unsigned long long * p = 0;
unsigned long long APRR(unsigned int m, unsigned int n) {
if (!m) return ++n;
unsigned long long r = p ? p[m] : APRR(m - 1, 1);
--m;
while(n--)
r = APRR(m, r);
return r;
}
unsigned long long APRA(unsigned int m, unsigned int n) {
return
m ?
n ?
APRR(m, n)
: p ? p[m] : APRA(--m, 1)
: ++n
;
}
unsigned long long APR(unsigned int m, unsigned int n) {
unsigned long long r = 0;
// Allocate
p = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
// Initialize
for(; r <= m; ++r)
p[r] = r ? APRA(r - 1, 1) : APRA(r, 0);
// Calculate
r = APRA(m, n);
// Free
free(p);
return r;
}
unsigned long long AP(unsigned int m, unsigned int n) {
return APH(m, n);
return APR(m, n);
}
int main(int n, char ** a) {
unsigned int M, N;
if (n != 3) {
printf("Usage: %s <m> <n>\n", *a);
return 1;
}
printf("AckermannPeter(%u, %u) = %llu\n", M = atoi(a[1]), N = atoi(a[2]), AP(M, N));
//printf("\nPress any key...");
//getch();
return 0;
}

View file

@ -0,0 +1,147 @@
/* Thejaka Maldeniya */
#include <conio.h>
unsigned long long HI(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Iterative): Perform a Hyperoperation...
unsigned long long *I, r = 1;
unsigned int N = n - 3;
if (!N)
// Exponentiation
while(b--)
r *= a;
else if(b) {
n -= 2;
// Allocate
I = (unsigned long long *) malloc(sizeof(unsigned long long) * n--);
// Initialize
I[n] = b;
// Calculate
for(;;) {
if(I[n]) {
--I[n];
if (n)
I[--n] = r, r = 1;
else
r *= a;
} else
for(;;)
if (n == N)
goto a;
else if(I[++n])
break;
}
a:
// Free
free(I);
}
return r;
}
unsigned long long H(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Iterative-O(1) Hybrid): Perform a Hyperoperation...
switch(n) {
case 0:
// Increment
return ++b;
case 1:
// Addition
return a + b;
case 2:
// Multiplication
return a * b;
}
return HI(n, a, b);
}
unsigned long long APH(unsigned int m, unsigned int n) {
// Ackermann-Péter Function (Recursive-Iterative-O(1) Hybrid)
return H(m, 2, n + 3) - 3;
}
unsigned long long * p = 0;
unsigned long long APIA(unsigned int m, unsigned int n) {
if (!m) return ++n;
// Initialize
unsigned long long *I, r = p ? p[m] : APIA(m - 1, 1);
unsigned int M = m;
if (n) {
// Allocate
I = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
// Initialize
I[m] = n;
// Calculate
for(;;) {
if(I[m]) {
if (m)
--I[m], I[--m] = r, r = p ? p[m] : APIA(m - 1, 1);
else
r += I[m], I[m] = 0;
} else
for(;;)
if (m == M)
goto a;
else if(I[++m])
break;
}
a:
// Free
free(I);
}
return r;
}
unsigned long long API(unsigned int m, unsigned int n) {
unsigned long long r = 0;
// Allocate
p = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
// Initialize
for(; r <= m; ++r)
p[r] = r ? APIA(r - 1, 1) : APIA(r, 0);
// Calculate
r = APIA(m, n);
// Free
free(p);
return r;
}
unsigned long long AP(unsigned int m, unsigned int n) {
return APH(m, n);
return API(m, n);
}
int main(int n, char ** a) {
unsigned int M, N;
if (n != 3) {
printf("Usage: %s <m> <n>\n", *a);
return 1;
}
printf("AckermannPeter(%u, %u) = %llu\n", M = atoi(a[1]), N = atoi(a[2]), AP(M, N));
//printf("\nPress any key...");
//getch();
return 0;
}

View file

@ -0,0 +1,10 @@
(deffunction ackerman
(?m ?n)
(if (= 0 ?m)
then (+ ?n 1)
else (if (= 0 ?n)
then (ackerman (- ?m 1) 1)
else (ackerman (- ?m 1) (ackerman ?m (- ?n 1)))
)
)
)

View file

@ -0,0 +1,68 @@
(deffacts solve-items
(solve 0 4)
(solve 1 4)
(solve 2 4)
(solve 3 4)
)
(defrule acker-m-0
?compute <- (compute 0 ?n)
=>
(retract ?compute)
(assert (ackerman 0 ?n (+ ?n 1)))
)
(defrule acker-n-0-pre
(compute ?m&:(> ?m 0) 0)
(not (ackerman =(- ?m 1) 1 ?))
=>
(assert (compute (- ?m 1) 1))
)
(defrule acker-n-0
?compute <- (compute ?m&:(> ?m 0) 0)
(ackerman =(- ?m 1) 1 ?val)
=>
(retract ?compute)
(assert (ackerman ?m 0 ?val))
)
(defrule acker-m-n-pre-1
(compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(not (ackerman ?m =(- ?n 1) ?))
=>
(assert (compute ?m (- ?n 1)))
)
(defrule acker-m-n-pre-2
(compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(ackerman ?m =(- ?n 1) ?newn)
(not (ackerman =(- ?m 1) ?newn ?))
=>
(assert (compute (- ?m 1) ?newn))
)
(defrule acker-m-n
?compute <- (compute ?m&:(> ?m 0) ?n&:(> ?n 0))
(ackerman ?m =(- ?n 1) ?newn)
(ackerman =(- ?m 1) ?newn ?val)
=>
(retract ?compute)
(assert (ackerman ?m ?n ?val))
)
(defrule acker-solve
(solve ?m ?n)
(not (compute ?m ?n))
(not (ackerman ?m ?n ?))
=>
(assert (compute ?m ?n))
)
(defrule acker-solved
?solve <- (solve ?m ?n)
(ackerman ?m ?n ?result)
=>
(retract ?solve)
(printout t "A(" ?m "," ?n ") = " ?result crlf)
)

View file

@ -0,0 +1,19 @@
% Ackermann function
ack = proc (m, n: int) returns (int)
if m=0 then return(n+1)
elseif n=0 then return(ack(m-1, 1))
else return(ack(m-1, ack(m, n-1)))
end
end ack
% Print a table of ack( 0..3, 0..8 )
start_up = proc ()
po: stream := stream$primary_output()
for m: int in int$from_to(0, 3) do
for n: int in int$from_to(0, 8) do
stream$putright(po, int$unparse(ack(m,n)), 8)
end
stream$putl(po, "")
end
end start_up

View file

@ -0,0 +1,32 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Ackermann.
DATA DIVISION.
LINKAGE SECTION.
01 M USAGE UNSIGNED-LONG.
01 N USAGE UNSIGNED-LONG.
01 Return-Val USAGE UNSIGNED-LONG.
PROCEDURE DIVISION USING M N Return-Val.
EVALUATE M ALSO N
WHEN 0 ALSO ANY
ADD 1 TO N GIVING Return-Val
WHEN NOT 0 ALSO 0
SUBTRACT 1 FROM M
CALL "Ackermann" USING BY CONTENT M BY CONTENT 1
BY REFERENCE Return-Val
WHEN NOT 0 ALSO NOT 0
SUBTRACT 1 FROM N
CALL "Ackermann" USING BY CONTENT M BY CONTENT N
BY REFERENCE Return-Val
SUBTRACT 1 FROM M
CALL "Ackermann" USING BY CONTENT M
BY CONTENT Return-Val BY REFERENCE Return-Val
END-EVALUATE
GOBACK
.

View file

@ -0,0 +1,8 @@
proc A(m:int, n:int):int {
if m == 0 then
return n + 1;
else if n == 0 then
return A(m - 1, 1);
else
return A(m - 1, A(m, n - 1));
}

View file

@ -0,0 +1,8 @@
ackermann(m, n) {
if(m == 0)
return n + 1;
if(n == 0)
return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}

View file

@ -0,0 +1,4 @@
(defn ackermann [m n]
(cond (zero? m) (inc n)
(zero? n) (ackermann (dec m) 1)
:else (ackermann (dec m) (ackermann m (dec n)))))

View file

@ -0,0 +1,4 @@
ackermann = (m, n) ->
if m is 0 then n + 1
else if m > 0 and n is 0 then ackermann m - 1, 1
else ackermann m - 1, ackermann m, n - 1

View file

@ -0,0 +1,17 @@
0010 //
0020 // Ackermann function
0030 //
0040 FUNC a#(m#,n#)
0050 IF m#=0 THEN RETURN n#+1
0060 IF n#=0 THEN RETURN a#(m#-1,1)
0070 RETURN a#(m#-1,a#(m#,n#-1))
0080 ENDFUNC a#
0090 //
0100 // Print table of Ackermann values
0110 //
0120 ZONE 5
0130 FOR m#:=0 TO 3 DO
0140 FOR n#:=0 TO 4 DO PRINT a#(m#,n#),
0150 PRINT
0160 ENDFOR m#
0170 END

View file

@ -0,0 +1,4 @@
(defun ackermann (m n)
(cond ((zerop m) (1+ n))
((zerop n) (ackermann (1- m) 1))
(t (ackermann (1- m) (ackermann m (1- n))))))

View file

@ -0,0 +1,10 @@
(defun ackermann (m n)
(case m ((0) (1+ n))
((1) (+ 2 n))
((2) (+ n n 3))
((3) (- (expt 2 (+ 3 n)) 3))
(otherwise (ackermann (1- m) (if (zerop n) 1 (ackermann m (1- n)))))))
(loop for m from 0 to 4 do
(loop for n from (- 5 m) to (- 6 m) do
(format t "A(~d, ~d) = ~d~%" m n (ackermann m n))))

View file

@ -0,0 +1,29 @@
MODULE NpctAckerman;
IMPORT StdLog;
VAR
m,n: INTEGER;
PROCEDURE Ackerman (x,y: INTEGER):INTEGER;
BEGIN
IF x = 0 THEN RETURN y + 1
ELSIF y = 0 THEN RETURN Ackerman (x - 1 , 1)
ELSE
RETURN Ackerman (x - 1 , Ackerman (x , y - 1))
END
END Ackerman;
PROCEDURE Do*;
BEGIN
FOR m := 0 TO 3 DO
FOR n := 0 TO 6 DO
StdLog.Int (Ackerman (m, n));StdLog.Char (' ')
END;
StdLog.Ln
END;
StdLog.Ln
END Do;
END NpctAckerman.

View file

@ -0,0 +1,18 @@
Fixpoint ack (m : nat) : nat -> nat :=
fix ack_m (n : nat) : nat :=
match m with
| 0 => S n
| S pm =>
match n with
| 0 => ack pm 1
| S pn => ack pm (ack_m pn)
end
end.
(*
Example:
A(3, 2) = 29
*)
Eval compute in ack 3 2.

View file

@ -0,0 +1,13 @@
Require Import Utf8.
Section FOLD.
Context {A : Type} (f : A → A) (a : A).
Fixpoint fold (n : nat) : A :=
match n with
| O => a
| S k => f (fold k)
end.
End FOLD.
Definition ackermann : nat → nat → nat :=
fold (λ g, fold g (g (S O))) S.

View file

@ -0,0 +1,14 @@
def ack(m, n)
if m == 0
n + 1
elsif n == 0
ack(m-1, 1)
else
ack(m-1, ack(m, n-1))
end
end
#Example:
(0..3).each do |m|
puts (0..6).map { |n| ack(m, n) }.join(' ')
end

View file

@ -0,0 +1,11 @@
ulong ackermann(in ulong m, in ulong n) pure nothrow @nogc {
if (m == 0)
return n + 1;
if (n == 0)
return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}
void main() {
assert(ackermann(2, 4) == 11);
}

View file

@ -0,0 +1,44 @@
import std.stdio, std.bigint, std.conv;
BigInt ipow(BigInt base, BigInt exp) pure nothrow {
auto result = 1.BigInt;
while (exp) {
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
BigInt ackermann(in uint m, in uint n) pure nothrow
out(result) {
assert(result >= 0);
} body {
static BigInt ack(in uint m, in BigInt n) pure nothrow {
switch (m) {
case 0: return n + 1;
case 1: return n + 2;
case 2: return 3 + 2 * n;
//case 3: return 5 + 8 * (2 ^^ n - 1);
case 3: return 5 + 8 * (ipow(2.BigInt, n) - 1);
default: return (n == 0) ?
ack(m - 1, 1.BigInt) :
ack(m - 1, ack(m, n - 1));
}
}
return ack(m, n.BigInt);
}
void main() {
foreach (immutable m; 1 .. 4)
foreach (immutable n; 1 .. 9)
writefln("ackermann(%d, %d): %s", m, n, ackermann(m, n));
writefln("ackermann(4, 1): %s", ackermann(4, 1));
immutable a = ackermann(4, 2).text;
writefln("ackermann(4, 2)) (%d digits):\n%s...\n%s",
a.length, a[0 .. 94], a[$ - 96 .. $]);
}

View file

@ -0,0 +1,8 @@
function Ackermann(m, n : Integer) : Integer;
begin
if m = 0 then
Result := n+1
else if n = 0 then
Result := Ackermann(m-1, 1)
else Result := Ackermann(m-1, Ackermann(m, n-1));
end;

View file

@ -0,0 +1,13 @@
int A(int m, int n) => m==0 ? n+1 : n==0 ? A(m-1,1) : A(m-1,A(m,n-1));
main() {
print(A(0,0));
print(A(1,0));
print(A(0,1));
print(A(2,2));
print(A(2,3));
print(A(3,3));
print(A(3,4));
print(A(3,5));
print(A(4,0));
}

View file

@ -0,0 +1,23 @@
[ # todo: n 0 -- n+1 and break 2 levels
+ 1 + # n+1
q
] s1
[ # todo: m 0 -- A(m-1,1) and break 2 levels
+ 1 - # m-1
1 # m-1 1
lA x # A(m-1,1)
q
] s2
[ # todo: m n -- A(m,n)
r d 0=1 # n m(!=0)
r d 0=2 # m(!=0) n(!=0)
Sn # m(!=0)
d 1 - r # m-1 m
Ln 1 - # m-1 m n-1
lA x # m-1 A(m,n-1)
lA x # A(m-1,A(m,n-1))
] sA
3 9 lA x f

View file

@ -0,0 +1,9 @@
function Ackermann(m,n:Int64):Int64;
begin
if m = 0 then
Result := n + 1
else if n = 0 then
Result := Ackermann(m-1, 1)
else
Result := Ackermann(m-1, Ackermann(m, n - 1));
end;

View file

@ -0,0 +1,18 @@
/* Ackermann function */
proc ack(word m, n) word:
if m=0 then n+1
elif n=0 then ack(m-1, 1)
else ack(m-1, ack(m, n-1))
fi
corp;
/* Write a table of Ackermann values */
proc nonrec main() void:
byte m, n;
for m from 0 upto 3 do
for n from 0 upto 8 do
write(ack(m,n) : 5)
od;
writeln()
od
corp

View file

@ -0,0 +1,6 @@
define method ack(m == 0, n :: <integer>)
n + 1
end;
define method ack(m :: <integer>, n :: <integer>)
ack(m - 1, if (n == 0) 1 else ack(m, n - 1) end)
end;

View file

@ -0,0 +1,5 @@
def A(m, n) {
return if (m <=> 0) { n+1 } \
else if (m > 0 && n <=> 0) { A(m-1, 1) } \
else { A(m-1, A(m,n-1)) }
}

View file

@ -0,0 +1,12 @@
fun ackermann = int by int m, int n
return when(m == 0,
n + 1,
when(n == 0,
ackermann(m - 1, 1),
ackermann(m - 1, ackermann(m, n - 1))))
end
for int m = 0; m <= 3; ++m
for int n = 0; n <= 6; ++n
writeLine("Ackermann(" + m + ", " + n + ") = " + ackermann(m, n))
end
end

View file

@ -0,0 +1,54 @@
PROGRAM ACKERMAN
!
! computes Ackermann function
! (second version for rosettacode.org)
!
!$INTEGER
DIM STACK[10000]
!$INCLUDE="PC.LIB"
PROCEDURE ACK(M,N->N)
LOOP
CURSOR_SAVE(->CURX%,CURY%)
LOCATE(8,1)
PRINT("Livello Stack:";S;" ")
LOCATE(CURY%,CURX%)
IF M<>0 THEN
IF N<>0 THEN
STACK[S]=M
S+=1
N-=1
ELSE
M-=1
N+=1
END IF
CONTINUE LOOP
ELSE
N+=1
S-=1
END IF
IF S<>0 THEN
M=STACK[S]
M-=1
CONTINUE LOOP
ELSE
EXIT PROCEDURE
END IF
END LOOP
END PROCEDURE
BEGIN
PRINT(CHR$(12);)
FOR X=0 TO 3 DO
FOR Y=0 TO 9 DO
S=1
ACK(X,Y->ANS)
PRINT(ANS;)
END FOR
PRINT
END FOR
END PROGRAM

View file

@ -0,0 +1,12 @@
proc ackerm m n . r .
if m = 0
r = n + 1
elif n = 0
call ackerm m - 1 1 r
else
call ackerm m n - 1 h
call ackerm m - 1 h r
.
.
call ackerm 3 6 r
print r

View file

@ -0,0 +1,4 @@
def ackermann =
[ 0 N -> N + 1
| M 0 -> ackermann (M - 1) 1
| M N -> ackermann (M - 1) (ackermann M (N - 1)) ]

View file

@ -0,0 +1,23 @@
class
ACKERMAN_EXAMPLE
feature -- Basic Operations
ackerman (m, n: NATURAL): NATURAL
-- Recursively compute the n-th term of a series.
require
non_negative_m: m >= 0
non_negative_n: n >= 0
do
if m = 0 then
Result := n + 1
elseif m > 0 and n = 0 then
Result := ackerman (m - 1, 1)
elseif m > 0 and n > 0 then
Result := ackerman (m - 1, ackerman (m, n - 1))
else
check invalid_arg_state: False end
end
end
end

View file

@ -0,0 +1,3 @@
ack 0 n = n+1
ack m 0 = ack (m - 1) 1
ack m n = ack (m - 1) <| ack m <| n - 1

View file

@ -0,0 +1,30 @@
import extensions;
ackermann(m,n)
{
if(n < 0 || m < 0)
{
InvalidArgumentException.raise()
};
m =>
0 { ^n + 1 }
: {
n =>
0 { ^ackermann(m - 1,1) }
: { ^ackermann(m - 1,ackermann(m,n-1)) }
}
}
public program()
{
for(int i:=0, i <= 3, i += 1)
{
for(int j := 0, j <= 5, j += 1)
{
console.printLine("A(",i,",",j,")=",ackermann(i,j))
}
};
console.readChar()
}

View file

@ -0,0 +1,9 @@
defmodule Ackermann do
def ack(0, n), do: n + 1
def ack(m, 0), do: ack(m - 1, 1)
def ack(m, n), do: ack(m - 1, ack(m, n - 1))
end
Enum.each(0..3, fn m ->
IO.puts Enum.map_join(0..6, " ", fn n -> Ackermann.ack(m, n) end)
end)

View file

@ -0,0 +1,5 @@
(defun ackermann (m n)
(cond ((zerop m) (1+ n))
((zerop n) (ackermann (1- m) 1))
(t (ackermann (1- m)
(ackermann m (1- n))))))

View file

@ -0,0 +1,9 @@
-module(ackermann).
-export([ackermann/2]).
ackermann(0, N) ->
N+1;
ackermann(M, 0) ->
ackermann(M-1, 1);
ackermann(M, N) when M > 0 andalso N > 0 ->
ackermann(M-1, ackermann(M, N-1)).

View file

@ -0,0 +1,16 @@
>M=zeros(1000,1000);
>function map A(m,n) ...
$ global M;
$ if m==0 then return n+1; endif;
$ if n==0 then return A(m-1,1); endif;
$ if m<=cols(M) and n<=cols(M) then
$ M[m,n]=A(m-1,A(m,n-1));
$ return M[m,n];
$ else return A(m-1,A(m,n-1));
$ endif;
$endfunction
>shortestformat; A((0:3)',0:5)
1 2 3 4 5 6
2 3 4 5 6 7
3 5 7 9 11 13
5 13 29 61 125 253

View file

@ -0,0 +1,16 @@
function ack(atom m, atom n)
if m = 0 then
return n + 1
elsif m > 0 and n = 0 then
return ack(m - 1, 1)
else
return ack(m - 1, ack(m, n - 1))
end if
end function
for i = 0 to 3 do
for j = 0 to 6 do
printf( 1, "%5d", ack( i, j ) )
end for
puts( 1, "\n" )
end for

View file

@ -0,0 +1,38 @@
நிரல்பாகம் அகெர்மன்(முதலெண், இரண்டாமெண்)
@((முதலெண் < 0) || (இரண்டாமெண் < 0)) ஆனால்
பின்கொடு -1
முடி
@(முதலெண் == 0) ஆனால்
பின்கொடு இரண்டாமெண்+1
முடி
@((முதலெண் > 0) && (இரண்டாமெண் == 00)) ஆனால்
பின்கொடு அகெர்மன்(முதலெண் - 1, 1)
முடி
பின்கொடு அகெர்மன்(முதலெண் - 1, அகெர்மன்(முதலெண், இரண்டாமெண் - 1))
முடி
அ = int(உள்ளீடு("ஓர் எண்ணைத் தாருங்கள், அது பூஜ்ஜியமாகவோ, அதைவிடப் பெரியதாக இருக்கலாம்: "))
ஆ = int(உள்ளீடு("அதேபோல் இன்னோர் எண்ணைத் தாருங்கள், இதுவும் பூஜ்ஜியமாகவோ, அதைவிடப் பெரியதாகவோ இருக்கலாம்: "))
விடை = அகெர்மன்(அ, ஆ)
@(விடை < 0) ஆனால்
பதிப்பி "தவறான எண்களைத் தந்துள்ளீர்கள்!"
இல்லை
பதிப்பி "நீங்கள் தந்த எண்களுக்கான அகர்மென் மதிப்பு: ", விடை
முடி

View file

@ -0,0 +1,8 @@
let rec ackermann m n =
match m, n with
| 0, n -> n + 1
| m, 0 -> ackermann (m - 1) 1
| m, n -> ackermann (m - 1) ackermann m (n - 1)
do
printfn "%A" (ackermann (int fsi.CommandLineArgs.[1]) (int fsi.CommandLineArgs.[2]))

View file

@ -0,0 +1,7 @@
let ackermann M N =
let rec acker (m, n, k) =
match m,n with
| 0, n -> k(n + 1)
| m, 0 -> acker ((m - 1), 1, k)
| m, n -> acker (m, (n - 1), (fun x -> acker ((m - 1), x, k)))
acker (M, N, (fun x -> x))

View file

@ -0,0 +1,12 @@
[$$[%
\$$[%
1-\$@@a;! { i j -> A(i-1, A(i, j-1)) }
1]?0=[
%1 { i 0 -> A(i-1, 1) }
]?
\1-a;!
1]?0=[
%1+ { 0 j -> j+1 }
]?]a: { j i }
3 3 a;! . { 61 }

View file

@ -0,0 +1,22 @@
#APPTYPE CONSOLE
TestAckermann()
PAUSE
SUB TestAckermann()
FOR DIM m = 0 TO 3
FOR DIM n = 0 TO 10
PRINT AckermannF(m, n), " ";
NEXT
PRINT
NEXT
END SUB
FUNCTION AckermannF(m AS INTEGER, n AS INTEGER) AS INTEGER
IF NOT m THEN RETURN n + 1
IF NOT n THEN RETURN AckermannA(m - 1, 1)
RETURN AckermannC(m - 1, AckermannF(m, n - 1))
END FUNCTION
DYNC AckermannC(m AS INTEGER, n AS INTEGER) AS INTEGER

View file

@ -0,0 +1,11 @@
int Ackermann(int m, int n)
{
if (!m) return n + 1;
if (!n) return Ackermann(m - 1, 1);
return Ackermann(m - 1, Ackermann(m, n - 1));
}
int main(int m, int n)
{
return Ackermann(m, n);
}

View file

@ -0,0 +1,3 @@
END DYNC
DYNASM AckermannA(m AS INTEGER, n AS INTEGER) AS INTEGER

View file

@ -0,0 +1,35 @@
ENTER 0, 0
INVOKE Ackermann, m, n
LEAVE
RET
@Ackermann
ENTER 0, 0
.IF DWORD PTR [m] .THEN
JMP @F
.ENDIF
MOV EAX, n
INC EAX
JMP xit
@@
.IF DWORD PTR [n] .THEN
JMP @F
.ENDIF
MOV EAX, m
DEC EAX
INVOKE Ackermann, EAX, 1
JMP xit
@@
MOV EAX, n
DEC EAX
INVOKE Ackermann, m, EAX
MOV ECX, m
DEC ECX
INVOKE Ackermann, ECX, EAX
@xit
LEAVE
RET 8

Some files were not shown because too many files have changed in this diff Show more