{"id":83705,"date":"2021-08-23T09:00:39","date_gmt":"2021-08-23T03:30:39","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=83705"},"modified":"2021-08-23T09:00:39","modified_gmt":"2021-08-23T03:30:39","slug":"fibonacci-series","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/","title":{"rendered":"Fibonacci Series &#8211; Algorithm and Implementation"},"content":{"rendered":"<p>Fibonacci series is a special kind of series in which the next term is equal to the sum of the previous two terms. Thus, the initial two numbers of the series are always given to us.<\/p>\n<p>For example, let F0 and F1 denote the first two terms of the Fibonacci series. Then, let F0 = 0 and F1 = 1.<br \/>\nSuppose we wish to calculate 10 terms of this Fibonacci series. Then, the series will be:<br \/>\nF10 = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.<\/p>\n<h3>Methods to find Fibonacci Series:<\/h3>\n<p>There are various algorithms or methods by which we can find the Fibonacci series for a given set of terms. The most common methods are:<\/p>\n<p>1. Using recursion<\/p>\n<p>2. Without using recursion or using Dynamic programming<\/p>\n<p>3. Space optimized method in DP<\/p>\n<p>Let us see their implementations one by one.<\/p>\n<h3>Algorithm for Iterative Fibonacci Series:<\/h3>\n<p>The iterative approach is the dynamic programming approach. It makes use of a loop to perform the addition of the previous two terms. The algorithm for the iterative approach will be:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Procedure Iterative_Fibonacci(n):\n    int f0, f1, fib\n    f0 = 0\n    f1 = 1\n    display f0, f1\n    for int i := 1 to n:\n        fib := f0 + f1   \n        f0 := f1\n        f1 := fib\n        display fib\n    END for loop\nEND Iterative_Fibonacci\n<\/pre>\n<h3>Algorithm for Recursive Fibonacci Series:<\/h3>\n<p>We can also use recursion to generate the Fibonacci series. The pseudo-code for the recursive method is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Procedure Recursive_Fibonacci(n)\n    int f0, f1\n    f0 := 0\n    f1 := 1\n    if(n &lt;= 1):\n        return n\n    return Recursive_Fibonacci(n-1) + Recursive_Fibonacci(n-2)\nEND Recursive_Fibonacci\n<\/pre>\n<h3>Implementation Using DP(Iterative Approach):<\/h3>\n<h4>Implementation of Fibonacci Series in C:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int Iterative_fib(int n)\n{\n    int fib[n+2]; \n    fib[0] = 0;\n    fib[1] = 1;\n    for (int i = 2; i &lt;= n; i++)\n      fib[i] = fib[i-1] + fib[i-2];\n    return fib[n];\n}\n\nint main ()\n{\n    int n = 9;\n    printf(\"%d\", Iterative_fib(n));\n    return 0;\n}\n<\/pre>\n<h4>Fibonacci series Implementation in C++:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;bits\/stdc++.h&gt;\n\nint Iterative_fib(int n)\n{\n    int fib[n+2]; \n    fib[0] = 0;\n    fib[1] = 1;\n    for (int i = 2; i &lt;= n; i++)\n      fib[i] = fib[i-1] + fib[i-2];\n    return fib[n];\n}\n\nint main ()\n{\n    int n = 9;\n    cout&lt;&lt; Iterative_fib(n);\n    return 0;\n}\n<\/pre>\n<h4>Implementation of Fibonacci Series in Java:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Fibonacci\n{\n    static int Iterative_fib(int n)\n  {\n      int fib[] = new int[n+2]; \n      fib[0] = 0;\n      fib[1] = 1;\n  \n      for (int i = 2; i &lt;= n; i++)\n        fib[i] = fib[i-1] + fib[i-2];\n      return fib[n];\n  }\n  \n  public static void main (String args[])\n  {\n    int n = 10;\n    System.out.println(Iterative_fib(n));\n  }\n}\n<\/pre>\n<h4>Fibonacci series Implementation in Python:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def Iterative_fib(n):\n  fib = [0, 1]\n  for i in range(2, n+1):\n    f.append(fib[i-1] + fib[i-2])\n  return fib[n]\n  \nprint(Iterative_fib(10))\n<\/pre>\n<h3>Implementation Using Space Optimization in DP:<\/h3>\n<h4>Implementation of Fibonacci series in C:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;\nint SO_fib(int n)\n{\n    int num1 = 0, num2 = 1, num3;\n    if( n == 0)\n      return num1;\n    for (int i = 2; i &lt;= n; i++)\n    {\n      num3 = num1 + num2;\n      num1 = num2;\n      num2 = num3;\n    }\n    return num2;\n}\n\nint main ()\n{\n    int n = 10;\n    printf(\"%d\", SO_fib(n));\n    return 0;\n}\n<\/pre>\n<h4>Fibonacci series Implementation in C++:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;bits\/stdc++.h&gt;\nint SO_fib(int n)\n{\n    int num1 = 0, num2 = 1, num3;\n    if( n == 0)\n      return num1;\n    for (int i = 2; i &lt;= n; i++)\n    {\n      num3 = num1 + num2;\n      num1 = num2;\n      num2 = num3;\n    }\n    return num2;\n}\n\nint main ()\n{\n    int n = 10;\n    cout &lt;&lt; SO_fib(n));\n    return 0;\n}\n<\/pre>\n<h4>Implementation of Fibonacci series in Java:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Fibonacci\n{\n  static int SO_fib(int n)\n  {\n    int num1 = 0, num2 = 1, num3;\n    if (n == 0)\n      return num1;\n    for (int i = 2; i &lt;= n; i++)\n    {\n      num3 = num1 + num2;\n      num1 = num2;\n      num2 = num3;\n    }\n    return num2;\n  }\n\n  public static void main (String args[])\n  {\n    int n = 10;\n    System.out.println(SO_fib(n));\n  }\n}\n<\/pre>\n<h4>Fibonacci series Implementation in Python:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def SO_fib(n):\n  num1 = 0\n  num2 = 1\n  if n &lt; 0:\n    print(\"Incorrect input\")\n  elif n == 0:\n    return num1\n  elif n == 1:\n    return num2\n  else:\n    for i in range(2, n+1):\n      num3 = num1 + num2\n      num1 = num2\n      num2 = num3\n    return num2\n\nprint(SO_fib(10))\n<\/pre>\n<h3>Implementation Using Recursion:<\/h3>\n<h4>Implementation of Fibonacci series in C:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;\nint Recursive_fib(int n)\n{\n    if (n &lt;= 1)\n      return n;\n    return Recursive_fib(n-1) + Recursive_fib(n-2);\n}\n\nint main ()\n{\n    int n = 10;\n    printf(\"%d\", Recursive_fib(n));\n    return 0;\n}\n<\/pre>\n<h4>Fibonacci series Implementation in C++:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;bits\/stdc++.h&gt;\nint Recursive_fib(int n)\n{\n    if (n &lt;= 1)\n      return n;\n    return Recursive_fib(n-1) + Recursive_fib(n-2);\n}\n\nint main ()\n{\n    int n = 10;\n    cout &lt;&lt; Recursive_fib(n);\n    return 0;\n}\n<\/pre>\n<h4>Implementation of Fibonacci Series in Java:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Fibonacci\n{\n  static int Recursive_fib(int n)\n  {\n      if (n &lt;= 1)\n      return n;\n      return Recursive_fib(n-1) + Recursive_fib(n-2);\n  }\n  \n  public static void main (String args[])\n  {\n      int n = 9;\n      System.out.println(Recursive_fib(n));\n  }\n}\n<\/pre>\n<h4>Fibonacci series Implementation in Python:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def Recursive_fib(n):\n  if n&lt;0:\n    print(\"Incorrect input\")\n  elif n==0:\n    return 0\n  elif n==1:\n    return 1\n  else:\n    return Recursive_fib(n-1) + Recursive_fib(n-2)\n\nprint(Recursive_fib(10))\n<\/pre>\n<h3>Complexity Analysis of Fibonacci series:<\/h3>\n<p>Different methods generate different complexities for the same problem. These complexities are:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Method<\/b><\/td>\n<td><b>Time complexity<\/b><\/td>\n<td><b>Space complexity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Using recursion<\/span><\/td>\n<td><span style=\"font-weight: 400\">T(n) = T(n-1) + T(n-2)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Using DP<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Space optimization of DP<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Using the power of matrix method<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Optimized matrix method<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Recursive method in O(log n) time<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Using direct formula<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">DP using memoization<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have defined the Fibonacci series. We have also seen various methods with which we can implement it. We have implemented these methods in C, C++, Java and Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fibonacci series is a special kind of series in which the next term is equal to the sum of the previous two terms. Thus, the initial two numbers of the series are always given&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84163,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3555],"tags":[4088,4089,4090],"class_list":["post-83705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure","tag-fibonacci-series","tag-fibonacci-series-algorithm","tag-fibonacci-series-implementation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Fibonacci Series - Algorithm and Implementation - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is fibonacci series, different methods to find the series, its algorithm and its implementation in C, C++, Java and Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fibonacci Series - Algorithm and Implementation - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is fibonacci series, different methods to find the series, its algorithm and its implementation in C, C++, Java and Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-23T03:30:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fibonacci Series - Algorithm and Implementation - TechVidvan","description":"Learn what is fibonacci series, different methods to find the series, its algorithm and its implementation in C, C++, Java and Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/","og_locale":"en_US","og_type":"article","og_title":"Fibonacci Series - Algorithm and Implementation - TechVidvan","og_description":"Learn what is fibonacci series, different methods to find the series, its algorithm and its implementation in C, C++, Java and Python.","og_url":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-08-23T03:30:39+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Fibonacci Series &#8211; Algorithm and Implementation","datePublished":"2021-08-23T03:30:39+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/"},"wordCount":388,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg","keywords":["Fibonacci Series","Fibonacci Series Algorithm","Fibonacci Series Implementation"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/","url":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/","name":"Fibonacci Series - Algorithm and Implementation - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg","datePublished":"2021-08-23T03:30:39+00:00","description":"Learn what is fibonacci series, different methods to find the series, its algorithm and its implementation in C, C++, Java and Python.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/TV-Fibonnici-series-in-Data-Structure.jpg","width":1200,"height":628,"caption":"Fibonacci series in Data Structure"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/fibonacci-series\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Fibonacci Series &#8211; Algorithm and Implementation"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=83705"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83705\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84163"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=83705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=83705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=83705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}