Advertisement
roronoa

fibonacci avec memoïsation

Sep 8th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. map = new Map()
  2. f = (n) =>
  3. {
  4.     if(n <= 2)
  5.         return 0
  6.     if(n == 3)
  7.         return 1
  8.    
  9.    
  10.     if(!map.get(n))
  11.     {
  12.         x = f(n-1)+f(n-2)+f(n-3)+f(n-4)
  13.         map.set(n,x)
  14.         return x
  15.     }
  16.        
  17.     else return map.get(n)
  18.    
  19. }
  20. let n = readline()-1
  21. t = []
  22. for(i = 0; i <= n; i++)
  23.     t.push(f(i))
  24. //print(t)
  25.  
  26. console.log(t.reduce((m,n)=>m+n,0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement