1234567891011121314151617181920212223242526272829303132333435363738 |
- # Author: estelendur
- # Given a base b and a number of digits n, how many palindromic integers?
- # What proportion of possible integers are palindromes?
- # Can we learn anything interesting from this?
- from math import *
- def half(x):
- return ceil(float(x)/2)
- def pals(n, b):
- return (b-1)*(b**(half(n-1)))
- def num(n, b):
- return (b-1)*(b**(n-1))
- def prop (n, b):
- return pals(n, b)/num(n, b)
- def testex(nm, bm):
- if (nm==2 & bm==1):
- return ""
- elif (bm==1):
- return ""
- elif(nm==2):
- return ""
- else:
- return (str(nm)+" digits in base "+str(bm)+" gives "+
- str(floor(prop(nm,bm)*100))+" percent palindromes. "+
- test(nm-1,bm)+test(nm,bm-1))
- def test(nm, b):
- if(nm==2):
- return ""
- else:
- return(str(nm)+" digits: "+str(prop(nm,b)*100)+" percent palindromes. "
- +test(nm-1,b))
|