Advertisement
cardel

Ejercicio discretas 14 dic

Dec 14th, 2020
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1.  
  2.  
  3. def solucionRecursiva(n):
  4.   if n == 1:
  5.     return 3
  6.   elif n == 2:
  7.     return 8
  8.   else:
  9.     return 2*solucionRecursiva(n-1)+2*solucionRecursiva(n-2)
  10.  
  11. def solucionIterativa(n):
  12.   if n == 1:
  13.     return ["0","1","2"]
  14.   else:
  15.     solucionAnterior = solucionIterativa(n-1)
  16.     salida = []
  17.     for et in solucionAnterior:
  18.       salida.append(et+"0")
  19.       salida.append(et+"1")
  20.  
  21.       if et[-1]!="2":
  22.         salida.append(et+"2")
  23.  
  24.     return salida
  25.  
  26. for i in range(1,20):
  27.   print("n = ",i," fr(n) ",solucionRecursiva(i), " freal(n) ", len(solucionIterativa(i)))
  28.  
  29. #print(solucionIterativa(1))
  30. #print(solucionIterativa(4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement