avatarRashad Shirizada

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2804

Abstract

/ prints 13</span></pre></div><blockquote id="bea2"><p><b><i>Time Complexity:</i> </b>T(n) = T(n) which is linear.</p></blockquote><h2 id="a4f4">2. Dynamic programming</h2><p id="53ab">By saving the Fibonacci numbers determined so far, we may avoid the repetitive labor of approach 1.</p><div id="79e4"><pre>func fibonacci(num: <span class="hljs-built_in">Int</span>) -> <span class="hljs-built_in">Int</span> {</pre></div><div id="162e"><pre><span class="hljs-built_in">var</span> numbers = <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">repeating</span>: <span class="hljs-number">0</span>, count: num + <span class="hljs-number">1</span>)</pre></div><div id="7cef"><pre><span class="hljs-attribute">numbers</span>[<span class="hljs-number">0</span>] = <span class="hljs-number">0</span></pre></div><div id="37e0"><pre><span class="hljs-attribute">numbers</span>[<span class="hljs-number">1</span>] = <span class="hljs-number">1</span></pre></div><div id="746f"><pre><span class="hljs-function"><span class="hljs-title">for</span></span> i in <span class="hljs-number">2.</span>..num {</pre></div><div id="ae4b"><pre><span class="hljs-comment">//Add the previous 2 numbers</span></pre></div><div id="3dbe"><pre><span class="hljs-comment">// in the series and store it</span></pre></div><div id="6d37"><pre>numbers<span class="hljs-comment">[i]</span> = numbers<span class="hljs-comment">[i - 1]</span> + numbers<span class="hljs-comment">[i - 2]</span>;</pre></div><div id="5146"><pre>}</pre></div><div id="8f84"><pre><span class="hljs-keyword">return</span> numbers[<span class="hljs-built_in">num</span>]</pre></div><div id="a03f"><pre>}</pre></div><div id="fbf5"><pre><span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(fibonacci(num: <span class="hljs-number">7</span>)</span></span>) <span class="hljs-comment">// prints 13</span></pre></div><h2 id="c8f1">3. Optimized for Space Method 2</h2><p id="cc46">We can save space by keeping only the previous two numbers in method 2 because that is all we need to get the next Fibonacci number in the series.</p><div id="5a0c"><pre>func fibonacci(num: <span class="hljs-built_in">Int</span>) -> <span class="hljs-built_in">Int</span> {</pre></div><div id="78dc"><pre><span class="hljs-selector-tag">var</span> <span class="hljs-selector-tag">a</span> = <span class="hljs-number">0</span></pre></div><div id="ba2f"><pre><span class="hljs-selector-tag">var</span> <span class="hljs-selector-tag">b</span> = <span class="hljs-number">1</span></pre></div><div id="c72f"><pre><span class="hljs-built_in">var</span> <span class="hljs-symbol">c:</span> <span class="hljs-built_in">Int</span> = <span class="hljs-number">0</span></pre></div><div id="dd33"><pre><span class="hljs-function"><span

Options

class="hljs-title">for</span></span> _ in <span class="hljs-number">2.</span>..num {</pre></div><div id="024e"><pre><span class="hljs-attr">c</span> = a + b</pre></div><div id="17da"><pre><span class="hljs-attribute">a</span> <span class="hljs-operator">=</span> b</pre></div><div id="acbe"><pre><span class="hljs-attribute">b</span> <span class="hljs-operator">=</span> c</pre></div><div id="8a8b"><pre>}</pre></div><div id="3168"><pre><span class="hljs-keyword">return</span> b</pre></div><div id="3444"><pre>}</pre></div><div id="cccc"><pre><span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(fibonacci(num: <span class="hljs-number">7</span>)</span></span>) <span class="hljs-comment">// prints 13</span></pre></div><blockquote id="d40d"><p><b><i>Time Complexity:</i></b>O(n) <b><i>Extra Space: </i></b>O(1)</p></blockquote><h2 id="6b8b">4. Using formula</h2><p id="f862">In this method we directly implement the formula for nth term in the fibonacci series. Fn = {[(√5 + 1)/2] ^ n} / √5</p><p id="8ebf">Formula Reference is <a href="http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html">here</a>.</p><div id="4d10"><pre>func fibonacci(num: <span class="hljs-built_in">Int</span>) -> <span class="hljs-built_in">Int</span> {</pre></div><div id="1cca"><pre><span class="hljs-attribute">let</span> phi = (<span class="hljs-number">1</span> + sqrt(<span class="hljs-number">5</span>)) / <span class="hljs-number">2</span></pre></div><div id="dab0"><pre><span class="hljs-keyword">return</span> <span class="hljs-built_in">Int</span>( <span class="hljs-keyword">round</span>(pow(phi, <span class="hljs-keyword">Double</span>(num)) / <span class="hljs-built_in">sqrt</span>(<span class="hljs-number">5</span>)) )</pre></div><div id="132d"><pre>}</pre></div><div id="0d5d"><pre><span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(fibonacci(num: <span class="hljs-number">7</span>)</span></span>) <span class="hljs-comment">// prints 13</span></pre></div><blockquote id="625c"><p><b><i>Time Complexity: </i></b>O(logn), this is because calculating phi^n takes logn time <b><i>Space Complexity: </i></b>O(1)</p></blockquote><h2 id="c06d">Summary</h2><p id="649e">Fibonacci numbers are a series of whole numbers that go from 0 through 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. These numbers are sometimes known as the universal rule of nature or the secret code of nature. We can save space by keeping only the previous two numbers in method 1, which is all we need to get the next fibonacci number in the series.</p><p id="bac8"><b>Thanks</b></p><p id="f476">Much grateful to you for following me and my stories. Happy to have you here, and would like to make it worth your time.</p></article></body>

Fibonacci numbers exploration - 4 ways

Photo by Dan Burton on Unsplash

Fibonacci numbers are a series of numbers in which each number is the sum of the two numbers before it. As the first two numbers, it starts with 0 and 1. This sequence is a well-known mathematical formula. Fibonacci numbers can be seen in plant and animal structures. These numbers are sometimes known as the universal rule of nature or the secret code of nature. Let’s take a closer look at Fibonacci numbers in this article.

Fibonacci Numbers: What Are They?

Fibonacci numbers are a series of whole numbers that go from 0 through 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. Every number is the sum of the two numbers before it. Here are a few fascinating facts regarding Fibonacci numbers:

This sequence is known as the Fibonacci sequence, and it is endless. The Fibonacci series or sequence is represented by the letters Fn. If we build squares with certain widths, Fibonacci numbers can be represented as a spiral, as illustrated below. We can see how the squares fit together nicely in the diagram. For example, 5 plus 8 equals 13, 8 plus 13 equals 21, and so on.

We will discover 4 ways to find Fibonacci numbers:

1. Using Recursion

The method is a straightforward recursive implementation of the mathematical recurrence relation.

import Foundation
func fibonacci(num: Int) -> Int {
if num <= 1 {
return num
}
return fibonacci(num: num - 1) + fibonacci(num: num - 2)
}
print(fibonacci(num: 7)) // prints 13

Time Complexity: T(n) = T(n) which is linear.

2. Dynamic programming

By saving the Fibonacci numbers determined so far, we may avoid the repetitive labor of approach 1.

func fibonacci(num: Int) -> Int {
var numbers = Array(repeating: 0, count: num + 1)
numbers[0] = 0
numbers[1] = 1
for i in 2...num {
//Add the previous 2 numbers
// in the series and store it
numbers[i] = numbers[i - 1] + numbers[i - 2];
}
return numbers[num]
}
print(fibonacci(num: 7)) // prints 13

3. Optimized for Space Method 2

We can save space by keeping only the previous two numbers in method 2 because that is all we need to get the next Fibonacci number in the series.

func fibonacci(num: Int) -> Int {
var a = 0
var b = 1
var c: Int = 0
for _ in 2...num {
c = a + b
a = b
b = c
}
return b
}
print(fibonacci(num: 7)) // prints 13

Time Complexity:O(n) Extra Space: O(1)

4. Using formula

In this method we directly implement the formula for nth term in the fibonacci series. Fn = {[(√5 + 1)/2] ^ n} / √5

Formula Reference is here.

func fibonacci(num: Int) -> Int {
let phi = (1 + sqrt(5)) / 2
return Int( round(pow(phi, Double(num)) / sqrt(5)) )
}
print(fibonacci(num: 7)) // prints 13

Time Complexity: O(logn), this is because calculating phi^n takes logn time Space Complexity: O(1)

Summary

Fibonacci numbers are a series of whole numbers that go from 0 through 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. These numbers are sometimes known as the universal rule of nature or the secret code of nature. We can save space by keeping only the previous two numbers in method 1, which is all we need to get the next fibonacci number in the series.

Thanks

Much grateful to you for following me and my stories. Happy to have you here, and would like to make it worth your time.

Fibonacci
Numbers
Math
Technology
Software Development
Recommended from ReadMedium