473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can this program be shortened? Measuring program-length?

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.
Jul 10 '08 #1
3 1105
On 10 Jul., 21:57, "r.e.s." <r_e_s...@ZZZyahoo.comwrote:
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)

Jul 10 '08 #2
<wo**************@googlemail.comwrote ...
"r.e.s." <r_e_s...@ZZZyahoo.comwrote:
>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".

Jul 11 '08 #3
"r.e.s." <r_******@ZZZyahoo.comwrote ...
<wo**************@googlemail.comwrote ...
>"r.e.s." <r_e_s...@ZZZyahoo.comwrote:
>>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
>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".
I forgot to mention the obvious typos. Thanks again.
Jul 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: lucanos | last post by:
Hey All, Just wondering whether there is an abbreviated "If - Then - Else" format in PHP, much like that possible in JavaScript. JavaScript allows an abbreviated version in the following...
9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
3
by: jm.suresh | last post by:
I am trying to measure the time the processor spends on some operation, and I want this measure to not depend on the current load of the machine. But doing the following prints different values...
3
by: asoni | last post by:
Hello Experts, I need some advise in terms of measuring the MAX memory required to execute my tool written in C++ I have this struct which returns my memory usage in C++. int...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
1
by: urkel | last post by:
Dear all, I write a code in C and run it on Linux using icc compiler. However, there are too many iterations in the program and it takes so much time to complete just one run of the program (more...
2
by: pede69 | last post by:
Hi... Would someone please be kind enough to help me with php code to insert into my webpage? I have a site that shortens a url. It uses an SQL DB. (not important for this, I believe) I need...
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
3
by: Joseph | last post by:
I have an expression which is used in a form to change a text box to either read "Action" or "No Action", based on the state of a series of 12 check boxes and three text boxes. I want to use the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.