Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f
, which prints the Fibonacci numbers.
Starting point: 25 14 characters in Haskell:
f=0:1:zipWith(+)f(tail f)
f=0:scanl(+)1f
Generate the Fibonacci sequence. sequence SEQUENCE!
PDP-11 Assembler (source)
J, 27 characters for a non-recursive function:
+/
sums over a list.(,+/)
appends the sum of a list to its tail.}.@(,+/)
sums a list, appends an element to its tail, and drops the first element.}.@(,+/)^:y
iterates the above functiony
times.}.@(,+/)^:y(0 1x)
applies the above function to the list(0,1)
(thex
makes it an integer).{:}.@(,+/)^:y(0 1x)
takes the last element of the output list of the above.f=:3 :'{:}.@(,+/)^:y(0 1x)'
definesf
to be a function on one variabley
.Corrected after comments (thanks Sebastian), it wasn't a sequence solution, so here we go with 42 chars (includes the \n):
OLD post below
Python, 38 chars.
Not so short but the most readable in my opinion :P
EDIT: Here is the analytic way (if someone needs to see it in python :-)
Here's my best using scheme, in 45 characters:
RePeNt,
9, 8 charsOr 10 chars with printing:
Run using:
RePeNt is a stack based toy language I wrote (and am still improving) in which all operators/functions/blocks/loops use Reverse Polish Notation (RPN).
The answer is on the stack which builds itself up like:
It never terminates (it has the eqivilent of a C#/JAVA
do { } while(true)
loop) because the sequence will never terminate, but a terminating solution can be written thus:which is 12 chars.
I wonder if anyone will ever read this :(