Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: PICASyFAMAS
Views: 48
def cartesiano(A,n): fuf=[] for ii in range(n): fuf.append(A) return cartesian_product(fuf)
A={'a','b','c'} tutu=cartesiano(A,4) ('a','a','a','b') in tutu
True
tutu.cardinality()
81
B=range(3) tuta=cartesiano(B,4)
(0,0,1,2) in tuta def locus(pp):# exige que despues del cero vaya un 1 kk=len(pp) for ii in range(kk-1): if pp[ii]==0: if not(pp[ii+1]==1): return False# hay un 0 cuyo siguiente no es 1 if pp[kk-1]==0: #el último es cero return False return True
True
# cuántas palabras de longitud 4 cumplen locus ruta=[uu for uu in cartesiano(B,3) if locus(uu)]
print ruta
[(0, 1, 1), (0, 1, 2), (1, 0, 1), (1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 0, 1), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
len(ruta)
12 64
# cuántas palabras de longitud n cumplen locus def vale(nn): ruta=[uu for uu in cartesiano(B,nn) if locus(uu)] return len(ruta)
vale(3)
12
vale(4)
12
for ij in range(7): print vale(ij)
1 3 5 12 29 70 169
aa=matrix([[2,1],[1,0]]) var('n') aa^n
n [ 1/2*(sqrt(2) + 1)^n*(sqrt(1/2) + 1) - 1/2*(-sqrt(2) + 1)^n*(sqrt(1/2) - 1) 1/2*sqrt(1/2)*(sqrt(2) + 1)^n - 1/2*sqrt(1/2)*(-sqrt(2) + 1)^n] [1/2*(sqrt(2) + 1)^n*(sqrt(2) - 1)*(sqrt(1/2) + 1) + 1/2*(-sqrt(2) + 1)^n*(sqrt(2) + 1)*(sqrt(1/2) - 1) 1/2*sqrt(1/2)*(-sqrt(2) + 1)^n*(sqrt(2) + 1) + 1/2*sqrt(1/2)*(sqrt(2) + 1)^n*(sqrt(2) - 1)]
def locis(pp):# Busca un cero seguido de un 1 kk=len(pp) for ii in range(kk-1): if pp[ii]==0:#encontró el 0 if pp[ii+1]==1: return True# y le sigue 1 EUREKA! return False
locis([0,0,0,1,0])
True
locis([1,1, 2,1, 2,1,0])
False
def vile(nn): ruta=[uu for uu in cartesiano(B,nn) if locis(uu)] return len(ruta)
vile(6)
352
for ik in range(10): vile(ik)
0 0 1 6 26 99 352 1200 3977 12918
# nos preguntamos por las funciones con 3 elementos en el reccorido que suman k def suma(uu): # suma los elementos de uu conta=0 for mm in uu: conta+=mm return conta AA=range(3) for ii in range(1,8): sss=cartesiano(AA,ii) rr=vector(ZZ,ii*3) for uu in sss: toy=suma(uu) rr[toy]+=1 print rr
(1, 1, 1) (1, 2, 3, 2, 1, 0) (1, 3, 6, 7, 6, 3, 1, 0, 0) (1, 4, 10, 16, 19, 16, 10, 4, 1, 0, 0, 0) (1, 5, 15, 30, 45, 51, 45, 30, 15, 5, 1, 0, 0, 0, 0) (1, 6, 21, 50, 90, 126, 141, 126, 90, 50, 21, 6, 1, 0, 0, 0, 0, 0) (1, 7, 28, 77, 161, 266, 357, 393, 357, 266, 161, 77, 28, 7, 1, 0, 0, 0, 0, 0, 0)
def imagenes(uu): ron=[] for xx in uu: ron.append(xx) return Set(ron) imagenes((1,2,1,3,1,2,1))
{1, 2, 3}
def iny(uu): if imagenes(uu).cardinality()==len(uu): return True else: return False def inyectivas(n,m): AA=range(m) Qu=cartesiano(AA,n) gorda=[uu for uu in Qu if iny(uu)] return gorda
inyectivas(3,2)
[]
inyectivas(2,3)
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
inyectivas(2,4)
[(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
def cuentinyec(n,m): return len(inyectivas(n,m))
cuentinyec(3,10)
720
# vamos a contar palabras de 4 dígitos sin repetición que contienen el 8 melba=inyectivas(4,10) nico=[uu for uu in melba if 8 in imagenes(uu)]
len(nico)
2016
(3,2,4,5) in nico
False
(1,2,2,8) in nico
False
(2,6,4,8) in nico
True
AA=range(10) mel=cartesiano(AA,4) nic=[uu for uu in mel if 8 in imagenes(uu)]
len(nic)
3439
4*9^3+6*9^2+4*9+1
3439
naco=[uu for uu in mel if imagenes(uu).cardinality()==3]
len(naco)
4320
6*10*9*8
4320
naca=[uu for uu in mel if imagenes(uu).cardinality()==2] len(naca)
630
3*10*9+90*4
630
def sobre(uu,mm): if imagenes(uu).cardinality()==mm: return True else: return False
def sobrevas(n,m): AA=range(m) Qu=cartesiano(AA,n) gorda=[uu for uu in Qu if sobre(uu,m)] return gorda
sobrevas(4,2)
[(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0)]
sobrevas(3,3)
[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
len(sobrevas(4,3))
36
A=range(3) mel=cartesiano(A,4) nace=[uu for uu in mel if 1 in imagenes(uu)]
nace
[(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 0, 1, 2), (0, 0, 2, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 0, 2), (0, 1, 1, 0), (0, 1, 1, 1), (0, 1, 1, 2), (0, 1, 2, 0), (0, 1, 2, 1), (0, 1, 2, 2), (0, 2, 0, 1), (0, 2, 1, 0), (0, 2, 1, 1), (0, 2, 1, 2), (0, 2, 2, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 0, 2), (1, 0, 1, 0), (1, 0, 1, 1), (1, 0, 1, 2), (1, 0, 2, 0), (1, 0, 2, 1), (1, 0, 2, 2), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 0, 2), (1, 1, 1, 0), (1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 2, 0), (1, 1, 2, 1), (1, 1, 2, 2), (1, 2, 0, 0), (1, 2, 0, 1), (1, 2, 0, 2), (1, 2, 1, 0), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 2, 0), (1, 2, 2, 1), (1, 2, 2, 2), (2, 0, 0, 1), (2, 0, 1, 0), (2, 0, 1, 1), (2, 0, 1, 2), (2, 0, 2, 1), (2, 1, 0, 0), (2, 1, 0, 1), (2, 1, 0, 2), (2, 1, 1, 0), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 2, 0), (2, 1, 2, 1), (2, 1, 2, 2), (2, 2, 0, 1), (2, 2, 1, 0), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 2, 1)]
len(nace)
65
def cuentasobres(nn,mm): return len(sobrevas(nn,mm))
cuentasobres(8,4)
40824
4^8-4*(4-1)^8+binomial(4,2)*(4-2)^8-binomial(4,3)*(4-3)^8
40824
def F(n,m,k): #Yesid G = PotenciaProdCartesiano(range(m),n) #G:= [m]^[n] K = Set(range(k)) F_salida = [] for f in G: Conjunto = K.intersection(Set(set(f))) if Conjunto.cardinality()== k: F_salida.append(f) return F_salida def PotenciaProdCartesiano(A, n): #Yesid B =[] for i in range (n): B.append(A) return cartesian_product(B)
F(4,2,2)
[(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0)]
len(F(6,3,2))
602
def Stir2(n): RR=[[],[ 1 ]] for ii in range(2,n): nota=[0,1] for jj in range(2,ii+1): qq=RR[ii-1][jj-1]+jj*RR[ii-1][jj] nota.append(qq) nota.append(0) RR.append(nota) return RR
Stir2(6)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 6, in Stir2 IndexError: list index out of range
def Stirl2(n): RR=[[],[0,1,0]] for kk in range(2,n+1): SS=RR[kk-1] TT=[0] ll=len(SS) for ii in range(1,ll): qq=SS[ii-1]+ii*SS[ii] TT.append(qq) TT.append(0) RR.append(TT) return RR
Stirl2(4)
[[], [0, 1, 0], [0, 1, 1, 0], [0, 1, 3, 1, 0], [0, 1, 7, 6, 1, 0]]
for nono in range(10): print Stirl2(10)[nono]
[] [0, 1, 0] [0, 1, 1, 0] [0, 1, 3, 1, 0] [0, 1, 7, 6, 1, 0] [0, 1, 15, 25, 10, 1, 0] [0, 1, 31, 90, 65, 15, 1, 0] [0, 1, 63, 301, 350, 140, 21, 1, 0] [0, 1, 127, 966, 1701, 1050, 266, 28, 1, 0] [0, 1, 255, 3025, 7770, 6951, 2646, 462, 36, 1, 0]
1*binomial(5,1)+ 63*binomial(5,2)*2+ 301*binomial(5,3)*6+ 350*binomial(5,4)*24+binomial(5,5)* 140*120 5^7
78125 78125
7^5
16807
utu=Permutations(5)
len(utu)
120
for uu in Permutations(4): print uu
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
def escreci(uu): ti=len(uu) for ii in range(ti-1): if not uu[ii]<uu[ii+1]: return False break return True def nescre(n,m): A=range(m) rita=cartesiano(A,n) yeye=[] for uu in rita: if escreci(uu): yeye.append(uu) return yeye
escreci([1 ,5, 4, 6])
False
nescre(3,2)
[]
nescre(2,3)
[(0, 1), (0, 2), (1, 2)]
nescre(2,4)
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
for yoyo in range(4,12): print yoyo,len(nescre(4,yoyo))
4 1 5 5 6 15 7 35 8 70 9 126 10 210 11 330
def orbita(uu,jj): tt=[] while not jj in tt: tt.append(jj) yotas=uu[jj-1] jj=yotas return tt
orbita([2,3,5,4,1],1)
[1, 2, 3, 5]
for muchacha in Permutations(3): print muchacha,orbita(muchacha,2)
[1, 2, 3] [2] [1, 3, 2] [2, 3] [2, 1, 3] [2, 1] [2, 3, 1] [2, 3, 1] [3, 1, 2] [2, 1, 3] [3, 2, 1] [2]
def ciclos(uu): toma=[] for coco in uu: toma.append(Set(orbita(uu,coco))) return Set(toma)
ciclos([3,4,1,2,5])
{{1, 3}, {2, 4}, {5}}
Set([4,5,3,4,2,5])
{2, 3, 4, 5}
for muchacha in Permutations(4): print muchacha,ciclos(muchacha)
[1, 2, 3, 4] {{4}, {2}, {3}, {1}} [1, 2, 4, 3] {{3, 4}, {2}, {1}} [1, 3, 2, 4] {{4}, {2, 3}, {1}} [1, 3, 4, 2] {{2, 3, 4}, {1}} [1, 4, 2, 3] {{2, 3, 4}, {1}} [1, 4, 3, 2] {{2, 4}, {3}, {1}} [2, 1, 3, 4] {{1, 2}, {4}, {3}} [2, 1, 4, 3] {{1, 2}, {3, 4}} [2, 3, 1, 4] {{4}, {1, 2, 3}} [2, 3, 4, 1] {{1, 2, 3, 4}} [2, 4, 1, 3] {{1, 2, 3, 4}} [2, 4, 3, 1] {{3}, {1, 2, 4}} [3, 1, 2, 4] {{4}, {1, 2, 3}} [3, 1, 4, 2] {{1, 2, 3, 4}} [3, 2, 1, 4] {{1, 3}, {4}, {2}} [3, 2, 4, 1] {{1, 3, 4}, {2}} [3, 4, 1, 2] {{1, 3}, {2, 4}} [3, 4, 2, 1] {{1, 2, 3, 4}} [4, 1, 2, 3] {{1, 2, 3, 4}} [4, 1, 3, 2] {{3}, {1, 2, 4}} [4, 2, 1, 3] {{1, 3, 4}, {2}} [4, 2, 3, 1] {{1, 4}, {2}, {3}} [4, 3, 1, 2] {{1, 2, 3, 4}} [4, 3, 2, 1] {{2, 3}, {1, 4}}
def nciclos(nn,kk): #cuantas pertunaciones hay de orden nn con kk ciclos rotica=[uu for uu in Permutations(nn)if ciclos(uu).cardinality()==kk] return len(rotica)
nciclos(5,3)
35
print 0, for nn in range(5): print nciclos(5,nn+1),
0 24 50 35 10 1
print 0, for nn in range(6): print nciclos(6,nn+1),
0 120 274 225 85 15 1
120+274+225+85+15+1==factorial(6)
True
for ii in range(1,8): print 0, for nn in range(ii): print nciclos(ii,nn+1), print ''
0 1 0 1 1 0 2 3 1 0 6 11 6 1 0 24 50 35 10 1 0 120 274 225 85 15 1 0 720 1764 1624 735 175 21 1
def Stirl1(n): RR=[[],[0,1,0]] for kk in range(2,n+1): SS=RR[kk-1] TT=[0] ll=len(SS) for ii in range(1,ll): qq=SS[ii-1]+(kk-1)*SS[ii] TT.append(qq) TT.append(0) RR.append(TT) return RR
Stirl1(5)
[[], [0, 1, 0], [0, 1, 1, 0], [0, 2, 3, 1, 0], [0, 6, 11, 6, 1, 0], [0, 24, 50, 35, 10, 1, 0]]
def ImStirl1(n): RR=[[],[0,1,0]] for kk in range(2,n+1): SS=RR[kk-1] TT=[0] ll=len(SS) for ii in range(1,ll): qq=SS[ii-1]+(kk-1)*SS[ii] TT.append(qq) TT.append(0) RR.append(TT) print TT ImStirl1(10)
[0, 1, 1, 0] [0, 2, 3, 1, 0] [0, 6, 11, 6, 1, 0] [0, 24, 50, 35, 10, 1, 0] [0, 120, 274, 225, 85, 15, 1, 0] [0, 720, 1764, 1624, 735, 175, 21, 1, 0] [0, 5040, 13068, 13132, 6769, 1960, 322, 28, 1, 0] [0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1, 0] [0, 362880, 1026576, 1172700, 723680, 269325, 63273, 9450, 870, 45, 1, 0]