CHALLENGE: Shortest Algorithim for Fibonacci Sequence on ONE line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaemonCoder
    New Member
    • Mar 2008
    • 43

    CHALLENGE: Shortest Algorithim for Fibonacci Sequence on ONE line

    The challenge is to create the shortest algorithim, In any programming language.

    RULES:
    1. All code must be on one line.
    2. Languages that prevent this are disallowed. I.E Assembly, and of course COBOL. All other languages are fair game.
    3. Sequence must go up to at least 4181.


    If i missed a languge that should be disallowed please let me know...
    My solution is in Java. weighing in @ 108 Characters w/o spaces.

    Code:
     
    class F{public static void main(String[] a){int f=0; int t=1;while(f<4181){System.out.println(f);t=f+(f=t);}}
    ---
    Assembly - If there was a fbonacci contest for the least amout of byte code.. It would win every time....
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    should be able to do this, although it's bad style.
    int f=0,t=1;
    It's bad style not spacing your program out anyways.

    c++ should be shorter:
    editing...

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Can make it even shorter by making it a for loop instead:
      int a=1,b=1;for(;b< 4182;a=b+(b=a)) System.out.prin tln(b);

      Also, < has to be < 4182 if you want 4181 to print for your algorithm.

      Comment

      • MACKTEK
        New Member
        • Mar 2008
        • 40

        #4
        In C, the compiler will not let me use #include in the same line as any other.
        So, not sure if this is "acceptable " per your rules.
        Code:
        #include<stdio.h>
        int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
        If not, then
        Code:
        int main(){for(int i,j=1;i<2585;);i+=j;j+=i;}}
        will work, although you wont get any output, the series is there in the variables. And this should be ok since your rules did not specify output to the screen, just that a series was created.

        Comment

        • DaemonCoder
          New Member
          • Mar 2008
          • 43

          #5
          That is correct. I should also have specified that Preprocessor directives are not language code, so they do not count twords the end character count. And thankyou for pointing out my mistakes. I will be the first to admit that i am a novice programmer....

          So to answer you question...
          Code:
           int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
          The code does count for a total of 58 characters. If my count is off please let me know.

          Comment

          • DaemonCoder
            New Member
            • Mar 2008
            • 43

            #6
            Using the for loop, it is down to 85 characters. Still not as good as 58 though. Just goes to show you how uselessly verbose java is.

            Code:
             
            class f{public static void main(){int a=1,b=1;for(;b<4182;a=b+(b=a))System.out.println(b);
            Oh, and please post the character count. Will make this a bit easier and eliminate the guess work. Thank you.

            Comment

            • DaemonCoder
              New Member
              • Mar 2008
              • 43

              #7
              You know i have too much time on my hands... My signature will now compile in GCC. I really need a life.. The next challenge will have to be: How to best abuse the Preprocessor...

              Comment

              • improvcornartist
                Recognized Expert Contributor
                • May 2007
                • 303

                #8
                [CODE=ruby]c=s=1;99.times{ p c,s;c+=s;s+=c}[/CODE]
                30 characters w/o whitespace

                Comment

                • SpecialKay
                  New Member
                  • Mar 2008
                  • 109

                  #9
                  would it count if i just #defined a function, then called the function. Could get it down to 1 char.
                  if you are not counting the defines

                  Comment

                  • FrostLord
                    Banned
                    New Member
                    • Apr 2008
                    • 3

                    #10
                    Is the line one.I think the code is in number one but like coding this is the true code




                    Dim a as string
                    Dim b as integer
                    For Each b in a
                    Writeline.true= 1

                    Comment

                    • kaarthikeyapreyan
                      New Member
                      • Apr 2007
                      • 106

                      #11
                      Hey this is python

                      Code:
                      def f():
                        r=0
                        p=1
                        for i in range(20) :
                          print r
                          r=r+p
                          p=r-p
                      f()
                      there are around 48 characters w\o white space

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by DaemonCoder
                        So to answer you question...
                        Code:
                         int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
                        The code does count for a total of 58 characters. If my count is off please let me know.
                        1. That is 68 characters
                        2. That solution does not work, i needs to be initialised


                        Code:
                        int main(){for(int i=1,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
                        for 70 characters however it can be re-written in 69 characters like this
                        Code:
                        int main(){for(int i=1,j=1;i<4182;i+=j,j+=i){printf("%d %d ",i,j);}}
                        note these are all C++ solutions.

                        I'm likeing these solutions but they are not the shortest

                        Code:
                        void f(int a, int b){if(a>4182)return;printf("%d %d ",b,a);f(2*a+b,a+b);}int main(){f(1,1);}
                        
                        int main(int a, char**p){int b=a&65535,c=a>>16;if(a!=1)printf("%d %d ",b,c);if(b<4181)main(a*2+b*65535+c,0);}
                        
                        
                        int main(int a, char**p){int b=a==1?1:&#40;int)p;printf("%d %d ",b,a);if(b<4181)main(a+a+b,(void*)(a+b));}
                        at 95, 112 and 105 characters each.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by kaarthikeyaprey an
                          Hey this is python

                          Code:
                          def f():
                            r=0
                            p=1
                            for i in range(20) :
                              print r
                              r=r+p
                              p=r-p
                          f()
                          there are around 48 characters w\o white space
                          A. you can't do it in Python because you can put the code on 1 line unfortunately
                          B. Since in Python the spaces are part of the language syntax you should count the (makes the count 80), the code will not work without the spaces there are required. An analogy is count C or C++ code without the ; the code would not work without them they are a necessary part of the syntax. But this is just my opinion.

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            Here is a rework to 79 characters
                            Code:
                            f(a,b)int a,b;{printf("%d %d ",b,a);b<4181?f(2*a+b,a+b):0;}int main(){f(1,1);}

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              In my own private language RPL (*) I can do it like this:

                              [code=RPL]
                              0 1'{drop swap over+dup swriteln}18time s
                              [/code]

                              If the results can be left on the data stack it shortens to this:

                              [code=RPL]
                              0 1'{drop over over+}18times
                              [/code]

                              kind regards,

                              Jos

                              (*) RPL stands for Reverse Polish Lisp and at least one other person in this world
                              knows about it so there is a user community ;-)

                              Comment

                              Working...