CHALLENGE: Shortest Algorithim for Fibonacci Sequence on ONE line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AmberJain
    Recognized Expert Contributor
    • Jan 2008
    • 922

    #16
    Well, this is my solution in C------->

    [CODE=C]main(){for(int a=0,b=1,c;a<418 2;c=a,a=b,b=b+c )printf("%d",a) ;}[/code]

    COUNT=62 characters (including all characters in above code which are necessary to compile and run the program).

    Of course, we will need to include header file stdio.h for this code to compile, but I think we are not counting that.

    So this my solution with 62 characters in C.

    What are your views about it?

    ==============
    =AmbrNewlearner =
    ==============

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #17
      PHP won't win 'cause we gotta put a dollar sign in front of our variables ):

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        I can't even program in my own language; this is the latest version which leaves
        all the results on the stack:

        [code=rpl]
        0 1'{2dupn+}18tim es
        [/code]

        nineteen characters (if I'm able to count ;-)

        kind regards,

        Jos

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #19
          Originally posted by JosAH
          I can't even program in my own language; this is the latest version which leaves
          all the results on the stack:

          [code=rpl]
          0 1'{2dupn+}18tim es
          [/code]
          What is this language and what language have you implemented it in?

          Comment

          • AmberJain
            Recognized Expert Contributor
            • Jan 2008
            • 922

            #20
            Originally posted by Banfa
            What is this language and what language have you implemented it in?
            JosAH is probably talking about RPL. Check out REPLY #15 above (in the same thread) for more details.

            With regards,
            ============
            AmbrNewlearner
            ============

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by Banfa
              What is this language and what language have you implemented it in?
              I named it RPL (Reverse Polish Lisp). It's a combination of Forth and Lisp (that
              seems to be a contradiction but it isn't). I've implemented it in quite a few languages
              and the latest (and most mature) implementation uses Java version 1.6. One of
              the first versions was in C (it even ran on small PDAs ;-)

              If you're really interested drop me a PM and I'll send you the sources so you can
              play with it too (I even have a tiny IDE for it and the language is still a bit under
              construction).

              Personally I like its symbolic expression rewriting rules; I consider them a basis
              for a new CAS (Computer Algebra System). It can do quite amazing things already.

              I'd be happy to get a bit of critique and ideas on the entire thing.

              kind regards,

              Jos

              Comment

              • kadghar
                Recognized Expert Top Contributor
                • Apr 2007
                • 1302

                #22
                this can be done even in Excel's VBA (note im bored in the office)

                [CODE=vb]sub f():b=1:c=1:d=" ,":e="1,1":whil e c<4181:a=b:b=c: c=a+b:e=e & d & c:wend:msgbox e:end sub[/CODE]

                using 91 characters, including white spaces (only the needed ones)

                ^.^

                I'll see if i can optimize this

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #23
                  The code before can be shorter (89 chars)

                  [CODE=vb]sub f():b=1:c=1:d=" ,":e="1,1":whil e c<4181:a=b:b=c: c=a+b:e=e &d &c:wend:msgb ox e:end sub[/CODE]

                  Or you can use recurrence relation, in case you want another way to calculate it (isnt it beautiful?):

                  [CODE=vb]sub f():for i=1 to 19:a=a &"," &round(.4472*1. 61803^i--.618^i):next:ms gbox a:end sub[/CODE]

                  this one is 85 chars (including spaces).

                  Of course, VBA is not the best 'language' for this, but it works fine anyway.

                  Comment

                  • skumarnpglobal
                    New Member
                    • Jul 2008
                    • 2

                    #24
                    I think AmbrNewlearner did great job!

                    Comment

                    • nakiya
                      New Member
                      • Apr 2009
                      • 3

                      #25
                      I dont have a lisp compiler atm :D

                      (defun i (a b c)(progn (print c)(i b c (+ a b))))(i 0 1 1)

                      pardon the errors, but i am pretty sure it can b shorter

                      Comment

                      • Ian Fellows

                        #26
                        in R
                        c(0,sapply(1:59 ,function(n){k= 1:n;sum(choose( n-k,k-1))}))

                        57 characters

                        Comment

                        • dkmiller100
                          New Member
                          • May 2012
                          • 1

                          #27
                          in Haskell

                          In Haskell:

                          Code:
                          fb=0:scanl (+) 1 fb
                          This returns the entire list of fibonacci numbers

                          Comment

                          • joekillian
                            New Member
                            • Jul 2012
                            • 1

                            #28
                            Originally posted by dkmiller100
                            In Haskell:

                            Code:
                            fb=0:scanl (+) 1 fb
                            This returns the entire list of fibonacci numbers
                            Here it is in J language:

                            {:"1 +/\@|.^:(i.5000) 0 1

                            Comment

                            • 211368e
                              New Member
                              • Jun 2022
                              • 1

                              #29
                              Code:
                              a=b=1n;while(1)[a,b]=[b,a+b]
                              Written in Javascript. However, this goes 1, 2, 3, 5, 8, and does not start with 0, 1, 1.

                              Comment

                              Working...