Homework Solutions

1.

def t():

return '1'*16

So, around 25 characters, or 200 bits.

2.

def t():

i=1

while True:

print '1'*i

print '0'

This doesn't return a string, since there's really no easy way to return an infinite string in Python, but it prints indefinitely and takes up roughly 40 characters, or 320 bits.

3.

def t():

def b(n):

b=""

b+=str(n)+" bottle"

if n!=1:

b+="s"

b+=" of beer "

return b

s=""

for x in range(99,0,-1):

s+=b(x)+"on the wall "+b(x)+"Take one down pass it around, "+b(x-1)+"on the wall "

return s[0:-1]

This one's about 230 characters, or 1920 bits, but there may be a shorter solution.

4.

def t(n):

p=3.0

i=0

s=1

for i in range(1,n):

b=2*i

p+=s*4.0/(b*(b+1)*(b+2))

s=-1*s

return p

There are many algorithms for calculating pi; this one converges at a rate of approximately six digits every hundred iterations, and takes up about 800 bits.