Advertisement
Guest User

sympy and lru_cache faster!!

a guest
Jul 2nd, 2023
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. from sympy import sqrt, simplify
  2. from functools import lru_cache
  3.  
  4. memo = {
  5.         0:2,
  6.         1:(sqrt(6)-sqrt(2))/2
  7.     }
  8.    
  9. simplify = lru_cache(simplify)
  10.  
  11. def sinus_seq(n):
  12.     if n in memo:
  13.         return memo[n]
  14.     a1 = sinus_seq(n-1)
  15.     a2 = sinus_seq(n-2)
  16.     memo[n] = simplify((2*(sqrt(6)-sqrt(2))/4)*a1-a2)
  17.     return memo[n]
  18.  
  19. for i in range(30000):
  20.     print(f"{sinus_seq(i)}")
  21.    
  22.    
  23.    
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement