fork download
  1. def transpuesta(matriz):
  2. m = len(matriz)
  3. n = len(matriz[0])
  4. return [[matriz[j][i] for j in range(m)] for i in range(n)]
  5.  
  6. def producto_punto(vector1, vector2):
  7. return sum(vector1[i] * vector2[i] for i in range(len(vector1)))
  8.  
  9. def mult_mat(matriz1, matriz2):
  10. return [[producto_punto(fila, columna) for columna in transpuesta(matriz2)]
  11. for fila in matriz1]
  12.  
  13. def exp_log(a, n):
  14. if n == 0:
  15. return 1
  16. if n % 2 == 0:
  17. exp = exp_log(a, n // 2)
  18. return exp * exp
  19. return a * exp_log(a, n - 1)
  20.  
  21. def exp_mat(matriz, n):
  22. if n == 1:
  23. return matriz
  24. if n % 2 == 0:
  25. exp = exp_mat(matriz, n // 2)
  26. return mult_mat(exp, exp)
  27. return mult_mat(matriz, exp_mat(matriz, n - 1))
  28.  
  29.  
  30.  
  31.  
  32. def g(n):
  33. if n<2:return 2
  34. return g(n-1)+g(n-2)
  35.  
  36. def gExpMat(n):
  37. if n<2:return 2
  38.  
  39. coeficientes=[[1,1],[1,0]]
  40.  
  41. cbases=[[2],[2]]
  42.  
  43. return mult_mat( exp_mat(coeficientes,n-1),cbases)[0][0]
  44.  
  45. print(g(7))
  46. print(gExpMat(7))
  47.  
Success #stdin #stdout 0.04s 9612KB
stdin
Standard input is empty
stdout
42
42