13 chars of Golfscript: 2,~{..p@+. }do Update to explain the operation of the script: 2, makes an array of 0 1 ~ puts that array on the stack So, at the time we run the do, we start the stack off with 0 1 (1 at top of stack) The do loop: Each . Duplicates the top item of the stack; here, we do this twice (leaving us with 0 1 1 1 on initial run) p prints the topmost value (leaving us with 0 1 1) @ rotates the top 3 items in the stack, so that the third-topmost is at the top (1 1 0) + adds the top 2 items in the stack (leaving 1 1) .
Duplicates the top value, so that the do loop can check its truthiness (to determine whether to continue) Tracing this mentally a couple of loops will be enough to tell you that this does the required addition to generate the Fibonacci sequence values. Since GolfScript has bignums, there will never be an integer overflow, and so the top-of-stack value at the end of the do loop will never be 0. Thus, the script will run forever.
See the winning entry for stackoverflow. Com/questions/62188 :-) – Chris Jester-Young Oct 24 '08 at 19:35.
18 characters of English.. "Fibonacci Sequence" ok, I fail. :).
4 wow, 190 rep, out of 201. – Malfist Feb 12 '09 at 5:20.
1" 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). Command Explanation Stack ------- ----------- ----- 1 Push a 1 onto the stack 1 ↓ Push last stack value 1 1 Start a do-while loop 1 1 2? Push a two, then pop the 2 and copy the last 2 stack 1 1 1 1 items onto the stack + Add on the stack 1 1 2 ↓£ Push last stack value then print it 1 1 2 1 Push a 1 onto the stack 1 1 2 1 Pop value (1 in this case), if it is a 0 exit the loop 1 1 2 otherwise go back to the loop start.
The answer is on the stack which builds itself up like: 1 1 1 1 2 1 1 2 3 1 1 2 3 5 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: N_1↓nI{2? +} which is 12 chars. I wonder if anyone will ever read this :(.
Perl 6 - 22 characters sub f{1,1...{$^a+$^b}}.
Language: C++ Compiler Errors Characters: 205 #define t template struct #define you template struct f t g { int v0; }; t f { enum { v = f::v + f::v }; g x;}; you { enum { v = 1 }; }; you { enum { v = 0 }; }; int main() { f x; }.
X86 (C-callable) realmode, 14 bytes. Input is n on stack, returns Fn in AX. 59 31 C0 E3 08 89 C3 40 93 01 D8 E2 FB C3.
22 characters with dc: 1pdd5**v1++2/lxxdsxx Invoke with either: dc -e'1pdd5**v1++2/lxxdsxx' Or: echo '1pdd5**v1++2/lxxdsxx' | dc Note: not my work, poached from perlmonks.
J, 27 characters for a non-recursive function: f=:3 :'{:}. @(,+/)^:y(0 1x)' +/ 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 function y times. }. @(,+/)^:y(0 1x) applies the above function to the list (0,1) (the x makes it an integer).
{:}. @(,+/)^:y(0 1x) takes the last element of the output list of the above. F=:3 :'{:}.
@(,+/)^:y(0 1x)' defines f to be a function on one variable y.
1 Your iterative way is not very suitable for generating the Fibonacci's sequence. See stackoverflow. Com/questions/232861/fibonacci-code-golf#250041 – J.F.Sebastian Oct 30 '08 at 12:46 3 It's not iterative but analytic – Dario Oct 10 '09 at 19:56.
For the record: Lua (66 chars): function f(n)if nActually, the linear one is even shorter in Lua (thanks to multiple assignment)! JavaScript isn't so lucky and Java is worse, having to declare vars... Lua (60 chars): function f(n)a=1;b=0;for i=1,n do a,b=b,a+b end return be end JavaScript (60 chars): function f(n){a=1;b=i=0;for(;i++For completeness, here are the terminal recursive versions. Lua's one, using tail call, is as fast as the linear one (but 69 chars, it is the longest!) - need to call them with three params, n,1,0.
Lua (69 char, longer! ): function f(n,a,b)if nB:f(n-1,b,a+b)} Java (52 chars): int f(int n,int a,int b){return n.
Windows XP (and later versions) batch script. This batch function when given a single argument - amount, generates amount+1 Fibonacci numbers and returns them as a string (BATCH doesn't really have sets) in variable %r% (369 characters, or 347 characters - if we remove indentation): :f set i=0 set r=1 set n=1 set f=0 :l if %n% GTR %~1 goto e set f=%f% %r% set /A s=%i%+%r% set i=%r% set r=%s% set /A n+=1 goto l :e set r=%f% exit /B 0 And here's the complete script, to see it in action (just copy-past it into a CMD or BAT file and run it): @echo off call :ff 0 call :ff 1 call :ff 2 call :ff 3 call :ff 5 call :ff 10 call :ff 15 call :ff 20 exit /B 0 :ff call :f "%~1" echo %~1: %r% exit /B 0 :f set i=0 set r=1 set n=1 set f=0 :l if %n% GTR %~1 goto e set f=%f% %r% set /A s=%i%+%r% set i=%r% set r=%s% set /A n+=1 goto l :e set r=%f% exit /B 0.
Language: dc, Char count: 20 Shorter dc solution. Dc -e'1dfdsa+plarlbxdsbx.
Here's my best using scheme, in 45 characters: (let f((a 0)(b 1))(printf"~a,"b)(f b(+ a b))).
Generate the Fibonacci sequence. Sequence SEQUENCE!
1 That's not an answer... But the remark is valid. – PhiLho Dec 18 '09 at 20:27.
C# I'm seeing a lot of answers that don't actually generate the sequence, but instead give you only the fibonacci number at position *n using recursion, which when looped to generate the sequence gets increasingly slower at higher values of n. Using System; static void Main() { var x = Math. Sqrt(5); for (int n = 0; n WriteLine((Math.
Pow((1 + x) / 2, n) - Math. Pow((1 - x) / 2, n)) / p) ; }.
Let rec f l a be =function 0->a::l|1->b::l|n->f (a::l) be (a+b) (n-1) in f 1 1;; 80 characters, but truly generates the sequence, in linear time.
Andrea Ambu An iterative pythonic fibonacci()'s version should look something like that: def fibonacci(a=0, b=1): while True: yield be a, be = b, a+b.
MS Excel: 11 characters: =SUM(A1:A2) Type 1 in the top 2 cells, then put the above formula in cell A3. Copy the formula down the spreadsheet. Starts losing accuracy due to floating point rounding on row 74.
Exceeds 10^307 and overflows to a #NUM! Error on row 1477.
Befunge-93 31 chars Will output an infinite list of the Fibonacci numbers, from 0 upwards, separated by tabs (could be reduced to 29 chars by deleting 9, in the first row, at the expense of no whitespace between numbers). Unfortunately, all the Befunge-93 interpreters I've tried seem to overflow after 65k, so the output is only correct until and including 46368 (which is F24). #v::1p1>01g:.
\:01p+9,# > ^ Confirmed to work (with caveat above) with the Befunge-93 interpreter in Javascript and the Visual Befunge Applet Full. I'm proud to say this is a completely original work (i.e. I did not copy this code from anyone), and it's much shorter than the Befunge solution currently on Rosetta Code.
Delphi Prism (Delphi for . Net) f:func:=n->iif(n>1,f(n-1)+f(n-2),n) 49 chars.
The previous Ruby example won't work w/o either semicolons or newlines, so it's actually 32 chars. Here's the first example to actually output the sequence, not just return the value of a specified index. Ruby: 53 chars, including newlines: def f(n);nUpto 20 {|n|p f n} or if you want function that outputs a usable data structure, 71 chars: def f(n);nTo_i).
To_a. Map {|n| f(n)}.
BrainF**k: >+++++>+>+>+>+>>>>-+>+>>-- That'll generate the first 5. To generate more, replace the 5 + at the beginning with more: eg: >++++++++++++++++++++++>+>+>+>+>>>>-+>+>>--.
Not the shortest, but the fastest at the time of posting. :-) float f(float n) { return (pow(1+sqrt(5.0))/2.0),n) - pow(1+sqrt(5.0))/2.0),n)/sqrt(n)); }.
1 If your intent is to generate a sequence, you would not need to recalculate any results -- you'd be saving them, in the sequence. – ephemient Oct 24 '08 at 18:11.
Euphoria: 44 characters object f=1&1 loop do f&=f$+f$-1until 0 Keeps on generating until RAM or doubles run out.
Lucid f = 1 fby 1 fby f + prev f; 27 characters, including the spaces.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.