Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. static Long fibonacci(int m){
  2. if(m < 0) {
  3. throw new IllegalArgumentException("index must be >= 0");
  4. }
  5.  
  6. Long[] arr = new Long[m+1];
  7.  
  8. //TODO uzyć lambdy
  9. return fun.apply(m, arr);
  10. }
  11.  
  12. static BiFunction<Integer, Long[], Long> fun = (n, memo) ->{
  13. if(memo[n] != null){
  14. return memo[n];
  15. } else {
  16. Long val;
  17. if(n > 1){
  18. val = Main.fun.apply(n-1, memo) + Main.fun.apply(n-2, memo);
  19. }
  20. else {
  21. val = 1L;
  22. }
  23. memo[n] = val;
  24. return val;
  25. }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement