Advertisement
Revolucent

Finally the Greatest Fibonacci in J!

Jul 5th, 2015
3,639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 0.67 KB | None | 0 0
  1. NB. Generates y Fibonacci numbers starting with 0 1.
  2. NB. If the y argument is boxed, generates
  3. NB. Fibonacci numbers up to the given number.
  4. NB. Tribonacci numbers (and so on) can be generated
  5. NB. as follows: 0 0 1x fibonacci 1e3 or (<3) fibonacci 1e3.
  6. fibonacci =: 3 : 0 "0 1 0
  7.     0x 1x fibonacci y
  8. :
  9.   if. L.x do.
  10.     x=.1x,~0$~<:>x
  11.   end.
  12.   (,a)^:((>y)>#`(a=.+/@((-#x)&{.))@.(L.y))^:_ x
  13. )
  14.  
  15. NB. Examples:
  16.  
  17. NB. First 100 Fibonacci numbers
  18. fibonacci 100
  19.  
  20. NB. Fibonacci numbers up to 100
  21. fibonacci <100
  22.  
  23. NB. Two ways to do the first 10 Tribonacci numbers
  24. 0 0 1x fibonacci 10
  25. (<3) fibonacci 10
  26.  
  27. NB. The Quadribonacci numbers up to 1e6
  28. (<5) fibonacci <1e6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement