Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NB. Run as /path/to/jconsole Fib.ijs <n>
- NB. Fibonacci in 32 characters by Robert 'Probie' Offner
- NB. This is kind of a cop-out, since my chosen language has
- NB. primitives for calculating Taylor coefficients.
- NB. ".>2{ARGV reads the argument and converts it from a string to a number
- NB. i. makes an array from 0 to the argument on the right
- NB. The taylor expansion of fibonacci is s(x)=x/(1-x-x*x)
- NB. In J this is written as (x % ((1 - x) - *:x))
- NB. Because of how order of operations work in J, this is (x % (1 - x) - *:x)
- NB. But we can make this shorter, -.x is shorthand for 1-x in J
- NB. This gives us (x % -. x - *:x)
- NB. J does *not* treat functions as first class objects, so they can't
- NB. just be passed around to other functions. This allows for a useful
- NB. shorthand where we can just remove the references to x, making (%-.-*:)
- NB. an anonymous function which expects one argument (x) and returns
- NB. (x % -. x - *:x)
- NB. t. is a function which takes a function as an argument and returns a new
- NB. function (an adverb in J speak) that gives us the Taylor coefficents,
- NB. of the function it was given. Implicit broadcasting applies (%-.-*:)t.
- NB. to each element of the array produced by i.
- NB. echo&> prints each element of the array
- NB. Finally we require exit to stop J from opening in interactive mode
- NB. Final count:
- NB. 9 characters to read input
- NB. 12 characters to calculate the Fibonacci sequence up to the input
- NB. 6 characters to print
- NB. 5 characters of pointless overhead from J
- exit echo&>(%-.-*:)t.i.".>2{ARGV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement