September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,4 +1,26 @@
* 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

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,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,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,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

@ -1,30 +1,30 @@
import extensions.
import extensions;
ackermann(m,n)
[
if((n < 0)||(m < 0))
[
InvalidArgumentException new; raise
].
{
if(n < 0 || m < 0)
{
InvalidArgumentException.raise()
};
m =>
0 [ ^n + 1 ];
! [
0 { ^n + 1 }
: {
n =>
0 [ ^ackermann(m - 1,1) ];
! [ ^ackermann(m - 1,ackermann(m,n-1)) ]
]
]
0 { ^ackermann(m - 1,1) }
: { ^ackermann(m - 1,ackermann(m,n-1)) }
}
}
public program
[
0 to:3 do(:i)
[
0 to:5 do(:j)
[
console printLine("A(",i,",",j,")=",ackermann(i,j))
]
].
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
]
console.readChar()
}

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

@ -1,21 +1,18 @@
Public Function ackermann (m as Float, n as Float) as Float
If m = 0 then
return n + 1
end If
If n = 0 then
return ackermann(m - 1, 1)
end If
return ackermann(m - 1, ackermann(m, n - 1))
Public Function Ackermann(m As Float, n As Float) As Float
If m = 0 Then
Return n + 1
End If
If n = 0 Then
Return Ackermann(m - 1, 1)
End If
Return Ackermann(m - 1, Ackermann(m, n - 1))
End
Public Sub Main()
Dim m, n as Float
For m = 0 to 3
For n = 0 to 4
print "Ackerman(";m;",";n;")=";ackermann(m, n)
Next
Next
Dim m, n As Float
For m = 0 To 3
For n = 0 To 4
Print "Ackermann("; m; ", "; n; ") = "; Ackermann(m, n)
Next
Next
End

View file

@ -0,0 +1,389 @@
/*
* Source https://stackoverflow.com/a/51092690/5520417
*/
package matematicas;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Stack;
/**
* @author rodri
*
*/
public class IterativeAckermannMemoryOptimization extends Thread {
/**
* Max percentage of free memory that the program will use. Default is 10% since
* the majority of the used devices are mobile and therefore it is more likely
* that the user will have more opened applications at the same time than in a
* desktop device
*/
private static Double SYSTEM_MEMORY_LIMIT_PERCENTAGE = 0.1;
/**
* Attribute of the type IterativeAckermann
*/
private IterativeAckermann iterativeAckermann;
/**
* @param iterativeAckermann
*/
public IterativeAckermannMemoryOptimization(IterativeAckermann iterativeAckermann) {
super();
this.iterativeAckermann = iterativeAckermann;
}
/**
* @return
*/
public IterativeAckermann getIterativeAckermann() {
return iterativeAckermann;
}
/**
* @param iterativeAckermann
*/
public void setIterativeAckermann(IterativeAckermann iterativeAckermann) {
this.iterativeAckermann = iterativeAckermann;
}
public static Double getSystemMemoryLimitPercentage() {
return SYSTEM_MEMORY_LIMIT_PERCENTAGE;
}
/**
* Principal method of the thread. Checks that the memory used doesn't exceed or
* equal the limit, and informs the user when that happens.
*/
@Override
public void run() {
String operating_system = System.getProperty("os.name").toLowerCase();
if ( operating_system.equals("windows") || operating_system.equals("linux") || operating_system.equals("macintosh") ) {
SYSTEM_MEMORY_LIMIT_PERCENTAGE = 0.25;
}
while ( iterativeAckermann.getConsumed_heap() >= SYSTEM_MEMORY_LIMIT_PERCENTAGE * Runtime.getRuntime().freeMemory() ) {
try {
wait();
}
catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ( ! iterativeAckermann.isAlive() )
iterativeAckermann.start();
else
notifyAll();
}
}
public class IterativeAckermann extends Thread {
/*
* Adjust parameters conveniently
*/
/**
*
*/
private static final int HASH_SIZE_LIMIT = 636;
/**
*
*/
private BigInteger m;
/**
*
*/
private BigInteger n;
/**
*
*/
private Integer hash_size;
/**
*
*/
private Long consumed_heap;
/**
* @param m
* @param n
* @param invalid
* @param invalid2
*/
public IterativeAckermann(BigInteger m, BigInteger n, Integer invalid, Long invalid2) {
super();
this.m = m;
this.n = n;
this.hash_size = invalid;
this.consumed_heap = invalid2;
}
/**
*
*/
public IterativeAckermann() {
// TODO Auto-generated constructor stub
super();
m = null;
n = null;
hash_size = 0;
consumed_heap = 0l;
}
/**
* @return
*/
public static BigInteger getLimit() {
return LIMIT;
}
/**
* @author rodri
*
* @param <T1>
* @param <T2>
*/
/**
* @author rodri
*
* @param <T1>
* @param <T2>
*/
static class Pair<T1, T2> {
/**
*
*/
/**
*
*/
T1 x;
/**
*
*/
/**
*
*/
T2 y;
/**
* @param x_
* @param y_
*/
/**
* @param x_
* @param y_
*/
Pair(T1 x_, T2 y_) {
x = x_;
y = y_;
}
/**
*
*/
/**
*
*/
@Override
public int hashCode() {
return x.hashCode() ^ y.hashCode();
}
/**
*
*/
/**
*
*/
@Override
public boolean equals(Object o_) {
if ( o_ == null ) {
return false;
}
if ( o_.getClass() != this.getClass() ) {
return false;
}
Pair<?, ?> o = (Pair<?, ?>) o_;
return x.equals(o.x) && y.equals(o.y);
}
}
/**
*
*/
private static final BigInteger LIMIT = new BigInteger("6");
/**
* @param m
* @param n
* @return
*/
/**
*
*/
@Override
public void run() {
while ( hash_size >= HASH_SIZE_LIMIT ) {
try {
this.wait();
}
catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for ( BigInteger i = BigInteger.ZERO; i.compareTo(LIMIT) == - 1; i = i.add(BigInteger.ONE) ) {
for ( BigInteger j = BigInteger.ZERO; j.compareTo(LIMIT) == - 1; j = j.add(BigInteger.ONE) ) {
IterativeAckermann iterativeAckermann = new IterativeAckermann(i, j, null, null);
System.out.printf("Ackmermann(%d, %d) = %d\n", i, j, iterativeAckermann.iterative_ackermann(i, j));
}
}
}
/**
* @return
*/
public BigInteger getM() {
return m;
}
/**
* @param m
*/
public void setM(BigInteger m) {
this.m = m;
}
/**
* @return
*/
public BigInteger getN() {
return n;
}
/**
* @param n
*/
public void setN(BigInteger n) {
this.n = n;
}
/**
* @return
*/
public Integer getHash_size() {
return hash_size;
}
/**
* @param hash_size
*/
public void setHash_size(Integer hash_size) {
this.hash_size = hash_size;
}
/**
* @return
*/
public Long getConsumed_heap() {
return consumed_heap;
}
/**
* @param consumed_heap
*/
public void setConsumed_heap(Long consumed_heap) {
this.consumed_heap = consumed_heap;
}
/**
* @param m
* @param n
* @return
*/
public BigInteger iterative_ackermann(BigInteger m, BigInteger n) {
if ( m.compareTo(BigInteger.ZERO) != - 1 && m.compareTo(BigInteger.ZERO) != - 1 )
try {
HashMap<Pair<BigInteger, BigInteger>, BigInteger> solved_set = new HashMap<Pair<BigInteger, BigInteger>, BigInteger>(900000);
Stack<Pair<BigInteger, BigInteger>> to_solve = new Stack<Pair<BigInteger, BigInteger>>();
to_solve.push(new Pair<BigInteger, BigInteger>(m, n));
while ( ! to_solve.isEmpty() ) {
Pair<BigInteger, BigInteger> head = to_solve.peek();
if ( head.x.equals(BigInteger.ZERO) ) {
solved_set.put(head, head.y.add(BigInteger.ONE));
to_solve.pop();
}
else if ( head.y.equals(BigInteger.ZERO) ) {
Pair<BigInteger, BigInteger> next = new Pair<BigInteger, BigInteger>(head.x.subtract(BigInteger.ONE), BigInteger.ONE);
BigInteger result = solved_set.get(next);
if ( result == null ) {
to_solve.push(next);
}
else {
solved_set.put(head, result);
to_solve.pop();
}
}
else {
Pair<BigInteger, BigInteger> next0 = new Pair<BigInteger, BigInteger>(head.x, head.y.subtract(BigInteger.ONE));
BigInteger result0 = solved_set.get(next0);
if ( result0 == null ) {
to_solve.push(next0);
}
else {
Pair<BigInteger, BigInteger> next = new Pair<BigInteger, BigInteger>(head.x.subtract(BigInteger.ONE), result0);
BigInteger result = solved_set.get(next);
if ( result == null ) {
to_solve.push(next);
}
else {
solved_set.put(head, result);
to_solve.pop();
}
}
}
}
this.hash_size = solved_set.size();
System.out.println("Hash Size: " + hash_size);
consumed_heap = (Runtime.getRuntime().totalMemory() / (1024 * 1024));
System.out.println("Consumed Heap: " + consumed_heap + "m");
setHash_size(hash_size);
setConsumed_heap(consumed_heap);
return solved_set.get(new Pair<BigInteger, BigInteger>(m, n));
}
catch ( OutOfMemoryError e ) {
// TODO: handle exception
e.printStackTrace();
}
throw new IllegalArgumentException("The arguments must be non-negative integers.");
}
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
IterativeAckermannMemoryOptimization iterative_ackermann_memory_optimization = new IterativeAckermannMemoryOptimization(
new IterativeAckermann());
iterative_ackermann_memory_optimization.start();
}
}

View file

@ -0,0 +1,34 @@
function ack(integer m, integer n)
if m=0 then
return n+1
elsif m=1 then
return n+2
elsif m=2 then
return 2*n+3
elsif m=3 then
return power(2,n+3)-3
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
constant limit = 23,
fmtlens = {1,2,2,2,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8}
atom t0 = time()
printf(1," 0")
for j=1 to limit do
string fmt = sprintf(" %%%dd",fmtlens[j+1])
printf(1,fmt,j)
end for
printf(1,"\n")
for i=0 to 5 do
printf(1,"%d:",i)
for j=0 to iff(i>=4?5-i:limit) do
string fmt = sprintf(" %%%dd",fmtlens[j+1])
printf(1,fmt,{ack(i,j)})
end for
printf(1,"\n")
end for

View file

@ -0,0 +1,82 @@
-- demo\rosetta\Ackermann.exw
include mpfr.e
procedure ack(integer m, mpz n)
if m=0 then
mpz_add_ui(n, n, 1) -- return n+1
elsif m=1 then
mpz_add_ui(n, n, 2) -- return n+2
elsif m=2 then
mpz_mul_si(n, n, 2)
mpz_add_ui(n, n, 3) -- return 2*n+3
elsif m=3 then
if not mpz_fits_integer(n) then
-- As per Go: 2^MAXINT would most certainly run out of memory.
-- (think about it: a million digits is fine but pretty daft;
-- however a billion digits requires > addressable memory.)
integer bn = mpz_sizeinbase(n, 2)
throw(sprintf("A(m,n) had n of %d bits; too large",bn))
end if
integer ni = mpz_get_integer(n)
mpz_set_si(n, 8)
mpz_mul_2exp(n, n, ni) -- (n:=8*2^ni)
mpz_sub_ui(n, n, 3) -- return power(2,n+3)-3
elsif mpz_cmp_si(n,0)=0 then
mpz_set_si(n, 1)
ack(m-1,n) -- return ack(m-1,1)
else
mpz_sub_ui(n, n, 1)
ack(m,n)
ack(m-1,n) -- return ack(m-1,ack(m,n-1))
end if
end procedure
constant limit = 23,
fmtlens = {1,2,2,2,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8},
extras = {{3,100},{3,1e6},{4,2},{4,3}}
procedure ackermann_tests()
atom t0 = time()
atom n = mpz_init()
printf(1," 0")
for j=1 to limit do
string fmt = sprintf(" %%%dd",fmtlens[j+1])
printf(1,fmt,j)
end for
printf(1,"\n")
for i=0 to 5 do
printf(1,"%d:",i)
for j=0 to iff(i>=4?5-i:limit) do
mpz_set_si(n, j)
ack(i,n)
string fmt = sprintf(" %%%ds",fmtlens[j+1])
printf(1,fmt,{mpz_get_str(n)})
end for
printf(1,"\n")
end for
printf(1,"\n")
for i=1 to length(extras) do
integer {em, en} = extras[i]
mpz_set_si(n, en)
string res
try
ack(em,n)
res = mpz_get_str(n)
integer lr = length(res)
if lr>50 then
res[21..-21] = "..."
res &= sprintf(" (%d digits)",lr)
end if
catch e
-- ack(4,3), ack(5,1) and ack(6,0) all fail,
-- just as they should do
res = "***ERROR***: "&e[E_USER]
end try
printf(1,"ack(%d,%d) %s\n",{em,en,res})
end for
n = mpz_free(n)
printf(1,"\n")
printf(1,"ackermann_tests completed (%s)\n\n",{elapsed(time()-t0)})
end procedure
ackermann_tests()

View file

@ -1,37 +0,0 @@
--
-- Ackermann.exw
-- =============
--
-- optimised. still no bignum library, so ack(4,2), which is power(2,65536)-3, which is
-- apparently 19729 digits, and any above, are beyond (the CPU/FPU hardware) and this.
-- (replaced ack(atom,atom) with ack(int,int) since the former fares no better.)
function ack(integer m, integer n)
if m=0 then
return n+1
elsif m=1 then
return n+2
elsif m=2 then
return 2*n+3
elsif m=3 then
return power(2,n+3)-3
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
procedure Ackermann()
for i=0 to 3 do
for j=0 to 10 do
printf(1,"%5d",ack(i,j))
end for
puts(1,"\n")
end for
printf(1,"ack(4,1) %5d\n",ack(4,1))
-- printf(1,"ack(4,2) %5d\n",ack(4,2)) -- power function overflow
if getc(0) then end if
end procedure
Ackermann()

View file

@ -1,3 +1,5 @@
:- table ack/3. % memoization reduces the execution time of ack(4,1,X) from several
% minutes to about one second on a typical desktop computer.
ack(0, N, Ans) :- Ans is N+1.
ack(M, 0, Ans) :- M>0, X is M-1, ack(X, 1, Ans).
ack(M, N, Ans) :- M>0, N>0, X is M-1, Y is N-1, ack(M, Y, Ans2), ack(X, Ans2, Ans).

View file

@ -1,3 +1,6 @@
from functools import lru_cache
@lru_cache(None)
def ack2(M, N):
if M == 0:
return N + 1

View file

@ -1,2 +1,4 @@
scala> for ( m <- 0 to 3; n <- 0 to 6 ) yield ack(m,n)
res0: Seq.Projection[BigInt] = RangeG(1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 8, 3, 5, 7, 9, 11, 13, 15, 5, 13, 29, 61, 125, 253, 509)
val ackMap = new mutable.HashMap[(BigInt,BigInt),BigInt]
def ackMemo(m: BigInt, n: BigInt): BigInt = {
ackMap.getOrElseUpdate((m,n), ack(m,n))
}

View file

@ -0,0 +1,29 @@
(define (A m n)
(letrec ((A-stream
(cons-stream
(ints-from 1) ;; m = 0
(cons-stream
(ints-from 2) ;; m = 1
(cons-stream
;; m = 2
(stream-map (lambda (n)
(1+ (* 2 (1+ n))))
(ints-from 0))
(cons-stream
;; m = 3
(stream-map (lambda (n)
(- (knuth-up-arrow 2 (- m 2) (+ n 3)) 3))
(ints-from 0))
;; m = 4...
(stream-tail A-stream 3)))))))
(stream-ref (stream-ref A-stream m) n)))
(define (ints-from n)
(letrec ((ints-rec (cons-stream n (stream-map 1+ ints-rec))))
ints-rec))
(define (knuth-up-arrow a n b)
(let loop ((n n) (b b))
(cond ((= b 0) 1)
((= n 1) (expt a b))
(else (loop (-1+ n) (loop n (-1+ b)))))))

View file

@ -0,0 +1,9 @@
DEF ACK(M,N)
IF M==0 THEN
RETURN N+1
ELSEIF M>0 AND N==0 THEN
RETURN ACK(M-1,1)
ELSE
RETURN ACK(M-1,ACK(M,N-1))
ENDIF
END

View file

@ -0,0 +1,29 @@
Private Function Ackermann_function(m As Variant, n As Variant) As Variant
Dim result As Variant
Debug.Assert m >= 0
Debug.Assert n >= 0
If m = 0 Then
result = CDec(n + 1)
Else
If n = 0 Then
result = Ackermann_function(m - 1, 1)
Else
result = Ackermann_function(m - 1, Ackermann_function(m, n - 1))
End If
End If
Ackermann_function = CDec(result)
End Function
Public Sub main()
Debug.Print " n=",
For j = 0 To 7
Debug.Print j,
Next j
Debug.Print
For i = 0 To 3
Debug.Print "m=" & i,
For j = 0 To 7
Debug.Print Ackermann_function(i, j),
Next j
Debug.Print
Next i
End Sub