In [1]:
from itertools import product
In [2]:
def func(n, x, k):
result = set()
for suffix in product([1, 0], repeat=n):
suffix = str(suffix).strip('(,)').replace(', ', '')
if "0" * (k + 1) not in suffix:
if "0" * k in suffix:
if suffix.count('1') == (n - x):
result.add(suffix)
return result
In [3]:
print func(4,2,1)
In [5]:
print func(5,4,2)
In [6]:
print func(7,3,1)
In [7]:
def funcLen(n, x, k):
return func(n,x,k).__len__()
In [8]:
print funcLen(7,3,1)
In [9]:
def noConsec(n):
sum=1 #funcLen(n,0,0)
for x in range(n+1):
sum=sum+funcLen(n,x,1)
return sum
In [10]:
print noConsec(5), noConsec(4), noConsec(3)
In [11]:
print noConsec(8), noConsec(7), noConsec(6)
In [12]:
print noConsec(11), noConsec(10), noConsec(9)
In [13]:
def cardinality(n,):
sum=0
for x in range(n+1):
for k in range(x+1):
sum=sum+funcLen(n,x,k)
return sum
In [14]:
print cardinality(3)
In [15]:
print cardinality(4)
In [16]:
print cardinality(8)
In [ ]:
print cardinality(12)
In [24]:
print func(3,0,0)
In [25]:
def perm(n,x):
sum=0
for k in range(x+1):
sum=sum+funcLen(n,x,k)
return sum
In [26]:
print perm(4,2)
In [27]:
print perm(7,3)
In [28]:
print perm(7,4)
In [29]:
print func(3,2,1)
In [33]:
print funcLen(5,1,1)
In [34]:
print func(5,1,1)
In [35]:
print funcLen(8,3,3)
In [36]:
print funcLen(9,5,5)
In [37]:
print funcLen(5,0,0)
In [38]:
print funcLen(5,4,3)
In [39]:
print funcLen(8,5,4)
In [40]:
print funcLen(10,9,8)
In [41]:
print funcLen(6,2,1), "Exception x>=3"
In [42]:
print funcLen(9,5,0)
In [43]:
print funcLen(7,7,5)
In [44]:
def series(n,x,k):
lengthSeries=[]
for i in range(x,n+1):
lengthSeries.append(funcLen(i,x,k))
return lengthSeries
In [45]:
print series(10,0,0)
In [46]:
print series(10,1,1)
In [47]:
print series(10,2,1)
In [48]:
print series(10,2,2)
In [ ]:
print series(6,3,3)
In [49]:
from scipy.interpolate import *
In [50]:
def polynomial(list):
x=[i*1.0 for i in range(1,len(list))]
return lagrange(x,list)
In [51]:
print polynomial(series(10,5,2))
In [52]:
print polynomial(series(10,6,4))
In [55]:
print polynomial(series(14,7,1))
In [56]:
from fractions import Fraction
In [57]:
print Fraction(0.6667)
In [58]:
print Fraction(0.6667).limit_denominator(10000)
In [59]:
print Fraction(0.6667).limit_denominator(100)
In [60]:
print polynomial(series(10,5,2))
In [62]:
for i in polynomial(series(10,5,2)):
print i,
In [64]:
for coef in polynomial(series(10,5,4)):
print Fraction(coef).limit_denominator(100),
In [66]:
for coef in polynomial(series(14,7,1)):
print Fraction(coef).limit_denominator(1000),
In [67]:
def formula(n, x, k):
sum = 0
for i in range(0, k):
value=funcLen(n - i - 1, x - i, k)
if value>0:
print "F(",n-i-1,',',x-i,',',k,') is',value
sum = sum + value
for j in range(0, k + 1):
value=funcLen(n - k - 1, x - k, j)
if value>0:
print "F(",n-k-1,',',x-k,',',j,') is',value
sum = sum + value
value=funcLen(n, x, k)
print "Using formula F(",n,',',x,',',k,') is',value
if sum == value:
return True
return False
In [68]:
print formula(6,3,2)
In [69]:
print formula(10,7,5)
In [70]:
print formula(3,2,2)
In [71]:
print formula(2,1,1)
In [ ]: