<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Calvin Opiti on Medium]]></title>
        <description><![CDATA[Stories by Calvin Opiti on Medium]]></description>
        <link>https://medium.com/@opiticalvin?source=rss-108d9c2a54a8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Tn6Xh7_tHe8SJXRG</url>
            <title>Stories by Calvin Opiti on Medium</title>
            <link>https://medium.com/@opiticalvin?source=rss-108d9c2a54a8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:24:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@opiticalvin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Fibonacci Sequence with GO]]></title>
            <link>https://opiticalvin.medium.com/fibonacci-sequence-with-go-3eecb9ff2f57?source=rss-108d9c2a54a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/3eecb9ff2f57</guid>
            <category><![CDATA[fibonacci]]></category>
            <category><![CDATA[memoization]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Calvin Opiti]]></dc:creator>
            <pubDate>Wed, 03 Mar 2021 17:09:10 GMT</pubDate>
            <atom:updated>2021-03-03T17:12:05.043Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently decided to take some time and teach myself a new programming language. I mostly work with Python and occasionally, JavaScript for and in my profession. I am not a master of either, but out of curiosity, availability of some free time to myself, excitement brought about by challenges and of course, a nasty experience during an Amazon interview, I opted to pick and learn this new language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DnnkReGamr7ps_2HlGT_FQ.png" /></figure><p>Of course I could have picked other languages, be it human, like <strong><em>Xhosa </em></strong>or programming, like Java, C++ etc, but why I mainly opted for <a href="https://golang.org/">Golang</a> is a story for another day.</p><h3><strong>Fibonacci Sequence</strong></h3><p>From that nasty experience (that I will write about in the near future), the one thing that came out of that experience, especially as a self-taught developer, is the need to understand <em>data structures and algorithms. </em>And that is why I am writing this article, and a few more later on, to make this series on data structure and algorithms.</p><p><em>Fibonacci sequence</em>, one of the most common mathematical formulas out there, refers to a sequence of numbers in which each number is a sum of its two preceding numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*YYwRvmiafSo2Pz5WMNoDPw.png" /><figcaption>source: <a href="https://math.libretexts.org/Bookshelves/Applied_Mathematics/Book%3A_College_Mathematics_for_Everyday_Life_(Inigo_et_al)">https://math.libretexts.org/Bookshelves/Applied_Mathematics/Book%3A_College_Mathematics_for_Everyday_Life_(Inigo_et_al)</a></figcaption></figure><p>The implementation of this algorithm with Go can be achieved with the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42d7a2b7c9e6efdd891e18ae7a7fbea4/href">https://medium.com/media/42d7a2b7c9e6efdd891e18ae7a7fbea4/href</a></iframe><p>Give it a try on <a href="https://play.golang.org/p/oZ6eTTKuq5c">Go Playground</a></p><p>Fibonacci sequence and generation of the sequence numbers is a recursive exercise. Using the code sample above, as n increases, there is a significantly higher cost, in terms of time and space, required to run to completion this algorithm. This time and space complexity jargon used to measure efficiency in running of an algorithm is referred to as the <strong>Big O notation</strong>. Discussion on the Big O notation is beyond the scope of this article.</p><p>Question is, how do we optimize our script for faster compute times and reduced memory resource usage?</p><h3>Memoization</h3><p>This is a concept in computer science used to optimize and speed up expensive or costly processes by cache of results.</p><p>With regards to generation of Fibonacci sequence of numbers and the recursive nature of the algorithm, caching of outcome with every iteration helps speed up execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*b3xNvP4bWVtewNu9xTrD5w.png" /><figcaption>Fibonacci Tree Representation (source: <a href="https://inst.eecs.berkeley.edu/~cs61bl/r//cur/trees/fibtree5.png">https://inst.eecs.berkeley.edu/~cs61bl/r//cur/trees/fibtree5.png</a>)</figcaption></figure><p>Using the figure above, the recursive nature of the algorithm can be represented as below (<em>see what I did there? :-)</em> ):</p><pre>Fib(0) = 0<br>Fib(1) = 1<br>Fib(2) = Fib(1) + Fib(0)<br>Fib(3) = Fib(2) +Fib(1)<br>Fib(5) = Fib(4) + Fib(3)</pre><p>The computation of the 5th Fibonacci sequence number can be significantly made faster if the results of the 3rd and 4th terms were cached. Now imagine how much faster the 50th sequence number can be generated, considering all the previously stored/cached sequence values. Its as fast as retrieving the values from storage and summing them up.</p><p>Here is the implementation with memoization…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4994aa14df7e482ca51ec100d72306d/href">https://medium.com/media/f4994aa14df7e482ca51ec100d72306d/href</a></iframe><p><a href="https://play.golang.org/p/Q_bCLmCFGVJ">Go Playground link</a></p><p>Don’t take my word for it as regards how blazing fast the execution is. Give it a try.</p><h3>Conclusion</h3><p>There is no quicker, fun and better optimized method for learning a new language, than trying the syntax out with data structures and algorithms exercises. In addition to improved retention of the language syntax, it also helps one develop applications that are rather time and memory efficient.</p><p>In the example above, through memoization, we have managed to significantly speed up the computation of the algorithm. This definitely leaves the processing compute not exhausted (<em>lol</em>) but with more memory available to undertake other jobs ;-)</p><p>PS: — Though I’m a newbie, I’d greatly appreciate feedback. Thanks</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3eecb9ff2f57" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>