Advertisement
Revolucent

Even better Fibonacci in J

May 30th, 2015
3,725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 0.54 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.
  6. fibonacci =: 3 : 0 "0 1 0
  7.   0x 1x fibonacci y
  8. :
  9.   (,a)^:((>y)>#`(a=.+/@((-#x)&{.))@.(L.y))^:_ x
  10. )
  11.  
  12. NB. Examples:
  13.  
  14. NB. Generate the first 20 Fibonacci numbers:
  15. fibonacci 20
  16.  
  17. NB. Generate the first 20 Tribonacci numbers:
  18. 0 0 1x fibonacci 20
  19.  
  20. NB. Generate the Fibonacci numbers up to 1e8:
  21. fibonacci <1e8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement