Can this program be shortened? Measuring program-length?

R

r.e.s.

Can the following program be shortened? ...

def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n
h(9,9)

Note: Although it halts eventually in principle, this program can't be
expected to terminate on any machine in the universe, as it computes a
number larger than Graham's number -- assuming Python is extended (if
necessary?) to access unbounded storage.

Besides using one-letter names and no unneeded whitespace, can something
more be done to shorten it? ("Obfuscating" the code would be okay.)

Also, I'm not really sure how best to measure a program's length, but
this one is now 98 bytes long (or 102 bytes, depending on how newlines
are handled). Is there a better measure of program-length?

Thanks for any feedback.
 
W

wolfram.hinderer

Can the following program be shortened? ...

def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n
h(9,9)

Some ideas...

# h is your version
def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n

def g(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n

def f(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:]
return n

def e(n,m):
E=[n]
while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n
return n

# some tests
print h(1,1), h(2,1), h(0,2)
print g(1,1), g(2,1), g(0,2)
print f(1,1), f(2,1), f(0,2)
print e(1,1), e(2,1), e(0,2)
 
R

r.e.s.

r.e.s. said:
Can the following program be shortened? ...

def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n
h(9,9)

Some ideas...

# h is your version
def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n

def g(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n

def f(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:]
return n

def e(n,m):
E=[n]
while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n
return n

# some tests
print h(1,1), h(2,1), h(0,2)
print g(1,1), g(2,1), g(0,2)
print f(1,1), f(2,1), f(0,2)
print e(1,1), e(2,1), e(0,2)

Very instructive! Thank you for the "step-by-step".
 
R

r.e.s.

r.e.s. said:
r.e.s. said:
Can the following program be shortened? ...

def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n
h(9,9)

Some ideas...

# h is your version
def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n

def g(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
^
g
return n

def f(n,m):
E=n,
while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:]
^
f
return n

def e(n,m):
E=[n]
while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n
^
e
Very instructive! Thank you for the "step-by-step".

I forgot to mention the obvious typos. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top