Advertisement
zinch

Lattice paths count

Oct 27th, 2014
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.42 KB | None | 0 0
  1.  
  2. cache = []
  3.  
  4. def binomial(n, k) {
  5.  
  6.     if (n == k || k == 0) {
  7.         return 1G
  8.     }
  9.  
  10.     if (! cache[n]) {
  11.         cache[n] = []
  12.     }
  13.    
  14.     if (! cache[n][k]) {
  15.         cache[n][k] = binomial(n-1,k) + binomial(n-1,k-1)
  16.     }
  17.        
  18.     return cache[n][k]
  19. }
  20.  
  21. println "Lattice paths counts:"
  22. println "For 2x2 grid there are ${binomial(4,2)} paths"
  23. println "For 3x3 grid there are ${binomial(6,3)} paths"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement