Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Mean size of the largest palindrome problem.
- ## by Nicolau Werneck, 2012-01-15
- from pylab import *
- def largest_palindrome(x,num):
- ## Initialize palindrome candidates list with the unit palindromes
- seeds = [(1,k) for k in range(x)]
- ## Initial "even" palindrome middles
- seeds += [(2,k) for k in range(x-1) if num[k]==num[k+1]]
- while seeds != []:
- output = seeds[-1][0]
- newseeds = []
- for l,k in seeds:
- if k>0 and k+l<x-1:
- if num[k-1]==num[k+l]:
- newseeds.append((l+2,k-1))
- seeds = newseeds
- return output
- def iteration(x):
- num = randint(0,10,x)
- return largest_palindrome(x,num)
- if __name__ == '__main__':
- ion()
- Nsmp = 10000
- Lx = [3,4,5,6,7,8,9,10,32,100,316,1000]
- suptitle('Cumulative probabilities of largest palindromes', size=16, fontweight='bold')
- axes([.1,.2,.8,.7])
- kk=0
- Lll=[]
- for x in Lx:
- kk+=0.04
- cdf = sort([iteration(x) for k in range(Nsmp)])
- p=mgrid[1:Nsmp+1]/(Nsmp+1.)
- ll=plot(cdf+kk,p,'-')[0]
- Lll.append(ll)
- figlegend(Lll,Lx, 'lower center', ncol=6, title='Values of x (number of digits)')
- grid()
- plot([0,10],[.5,.5],'k--')
- xlim(0,10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement