Closed form Fibonacci Series











up vote
1
down vote

favorite












I am using Python to create a Fibonacci using this formula:



enter image description here



I have this recursive Fibonacci function:



def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))


To display it I am using this:



nterms = 10

if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])


How would I use an iterative function with this Fibonacci?



I've tried using this:



def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn


However this iterative function doesn't seem to have the same output as the recursive function.



The output of the recursive function:



Recursive Fibonacci Sequence:  [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]


The output of the iterative function:



Iterative Fibonacci Sequence:  [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]









share|improve this question




















  • 4




    If you have a formula, why do you need either recursion or iteration?
    – Scott Hunter
    Nov 11 at 0:13










  • I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
    – Sanchit Kumar
    Nov 11 at 0:18















up vote
1
down vote

favorite












I am using Python to create a Fibonacci using this formula:



enter image description here



I have this recursive Fibonacci function:



def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))


To display it I am using this:



nterms = 10

if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])


How would I use an iterative function with this Fibonacci?



I've tried using this:



def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn


However this iterative function doesn't seem to have the same output as the recursive function.



The output of the recursive function:



Recursive Fibonacci Sequence:  [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]


The output of the iterative function:



Iterative Fibonacci Sequence:  [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]









share|improve this question




















  • 4




    If you have a formula, why do you need either recursion or iteration?
    – Scott Hunter
    Nov 11 at 0:13










  • I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
    – Sanchit Kumar
    Nov 11 at 0:18













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using Python to create a Fibonacci using this formula:



enter image description here



I have this recursive Fibonacci function:



def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))


To display it I am using this:



nterms = 10

if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])


How would I use an iterative function with this Fibonacci?



I've tried using this:



def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn


However this iterative function doesn't seem to have the same output as the recursive function.



The output of the recursive function:



Recursive Fibonacci Sequence:  [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]


The output of the iterative function:



Iterative Fibonacci Sequence:  [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]









share|improve this question















I am using Python to create a Fibonacci using this formula:



enter image description here



I have this recursive Fibonacci function:



def recursive_fibonacci(n):
if n <= 1:
return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
else:
return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))


To display it I am using this:



nterms = 10

if nterms <= 0:
print("Please Enter a positive integer")
else:
print("Recursive Fibonacci Sequence: " ,
[recursive_fibonacci(i) for i in range(nterms)])
print("Iterative Fibonacci Sequence: " ,
[iterative_fib(i) for i in range(nterms)])


How would I use an iterative function with this Fibonacci?



I've tried using this:



def iterative_fib(n):
equation = lambda n: int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n))
if n <= 1:
return equation(n)
else:
a, b = 1, 2
for i in range(n):
fn = equation((i-a)+(i-b))
return fn


However this iterative function doesn't seem to have the same output as the recursive function.



The output of the recursive function:



Recursive Fibonacci Sequence:  [0, 2, 2, 4, 6, 10, 16, 26, 42, 68]


The output of the iterative function:



Iterative Fibonacci Sequence:  [0, 2, 2, 2, 3, 6, 13, 27, 58, 122]






python recursion iteration fibonacci






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 1:01









coldspeed

111k17101170




111k17101170










asked Nov 11 at 0:02









Roman Roshchuk

406




406








  • 4




    If you have a formula, why do you need either recursion or iteration?
    – Scott Hunter
    Nov 11 at 0:13










  • I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
    – Sanchit Kumar
    Nov 11 at 0:18














  • 4




    If you have a formula, why do you need either recursion or iteration?
    – Scott Hunter
    Nov 11 at 0:13










  • I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
    – Sanchit Kumar
    Nov 11 at 0:18








4




4




If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13




If you have a formula, why do you need either recursion or iteration?
– Scott Hunter
Nov 11 at 0:13












I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18




I think(@Roman correct me if I am wrong), what he wants is to print all the values of f(n) starting from 0 to n. That's why he is iterating.
– Sanchit Kumar
Nov 11 at 0:18












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










The equation you're trying to implement is the closed form Fibonacci series.



Closed form means that evaluation is a constant time operation.



g = (1 + 5**.5) / 2  # Golden ratio.
def fib(N):
return int((g**N - (1-g)**N) / 5**.5)


Contrast with,



def fib_iterative(N):
a, b, i = 0, 1, 2
yield from (a, b)
while i < N:
a, b = b, a + b
yield b
i += 1




And we have



>>> [fib(n) for n in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> list(fib_iterative(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]





share|improve this answer




























    up vote
    1
    down vote













    I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.



    Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.



    Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.



    Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.



    To illustrate, when n = 3, f_3 is calculated as -
    enter image description here






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244630%2fclosed-form-fibonacci-series%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      The equation you're trying to implement is the closed form Fibonacci series.



      Closed form means that evaluation is a constant time operation.



      g = (1 + 5**.5) / 2  # Golden ratio.
      def fib(N):
      return int((g**N - (1-g)**N) / 5**.5)


      Contrast with,



      def fib_iterative(N):
      a, b, i = 0, 1, 2
      yield from (a, b)
      while i < N:
      a, b = b, a + b
      yield b
      i += 1




      And we have



      >>> [fib(n) for n in range(10)]
      [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
      >>> list(fib_iterative(10))
      [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]





      share|improve this answer

























        up vote
        2
        down vote



        accepted










        The equation you're trying to implement is the closed form Fibonacci series.



        Closed form means that evaluation is a constant time operation.



        g = (1 + 5**.5) / 2  # Golden ratio.
        def fib(N):
        return int((g**N - (1-g)**N) / 5**.5)


        Contrast with,



        def fib_iterative(N):
        a, b, i = 0, 1, 2
        yield from (a, b)
        while i < N:
        a, b = b, a + b
        yield b
        i += 1




        And we have



        >>> [fib(n) for n in range(10)]
        [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
        >>> list(fib_iterative(10))
        [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]





        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The equation you're trying to implement is the closed form Fibonacci series.



          Closed form means that evaluation is a constant time operation.



          g = (1 + 5**.5) / 2  # Golden ratio.
          def fib(N):
          return int((g**N - (1-g)**N) / 5**.5)


          Contrast with,



          def fib_iterative(N):
          a, b, i = 0, 1, 2
          yield from (a, b)
          while i < N:
          a, b = b, a + b
          yield b
          i += 1




          And we have



          >>> [fib(n) for n in range(10)]
          [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
          >>> list(fib_iterative(10))
          [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]





          share|improve this answer












          The equation you're trying to implement is the closed form Fibonacci series.



          Closed form means that evaluation is a constant time operation.



          g = (1 + 5**.5) / 2  # Golden ratio.
          def fib(N):
          return int((g**N - (1-g)**N) / 5**.5)


          Contrast with,



          def fib_iterative(N):
          a, b, i = 0, 1, 2
          yield from (a, b)
          while i < N:
          a, b = b, a + b
          yield b
          i += 1




          And we have



          >>> [fib(n) for n in range(10)]
          [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
          >>> list(fib_iterative(10))
          [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 0:18









          coldspeed

          111k17101170




          111k17101170
























              up vote
              1
              down vote













              I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.



              Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.



              Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.



              Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.



              To illustrate, when n = 3, f_3 is calculated as -
              enter image description here






              share|improve this answer



























                up vote
                1
                down vote













                I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.



                Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.



                Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.



                Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.



                To illustrate, when n = 3, f_3 is calculated as -
                enter image description here






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.



                  Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.



                  Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.



                  Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.



                  To illustrate, when n = 3, f_3 is calculated as -
                  enter image description here






                  share|improve this answer














                  I think you've misunderstood the expression f_n for the Fibonacci sequence that you mention.



                  Notice that it is not a recurrence relation. It is a function of n, i.e., it provides the value of the n-th term when given an n.



                  Hence, you really don't have a recursive/iterative solution to generate the entire Fibonnaci sequence here.



                  Plugging in n as 0, 1, 2, 3.. provides the terms 0, 1, 1, 2, .. of the series.



                  To illustrate, when n = 3, f_3 is calculated as -
                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 0:35

























                  answered Nov 11 at 0:23









                  Shash

                  487




                  487






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244630%2fclosed-form-fibonacci-series%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma