<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Suraj&#039;s blog &#187; Fibonacci</title>
	<atom:link href="http://www.surajshrestha.com.np/tag/fibonacci/feed" rel="self" type="application/rss+xml" />
	<link>http://www.surajshrestha.com.np</link>
	<description>imagination more important than knowledge</description>
	<lastBuildDate>Sun, 08 Aug 2010 23:20:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Memoized version of Fibonacci number calculation using only one variable in Java</title>
		<link>http://www.surajshrestha.com.np/algorithm/memoized-version-of-fibonacci-number-calculation-using-only-one-variable.html</link>
		<comments>http://www.surajshrestha.com.np/algorithm/memoized-version-of-fibonacci-number-calculation-using-only-one-variable.html#comments</comments>
		<pubDate>Wed, 05 Aug 2009 04:01:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Brute Force]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Memoization]]></category>
		<category><![CDATA[Recursive]]></category>

		<guid isPermaLink="false">http://www.surajshrestha.com.np/uncategorized/memoized-version-of-fibonacci-number-calculation-using-only-one-variable.html</guid>
		<description><![CDATA[Fibonacci number can be calculated using different techniques. Most common recursive technique is brute force where we calculate Fibonacci number by repeatedly calling the method of numbers less by one and less by two. In this technique, we do not care the fact the previous Fibonacci number may have been calculated. However, in Memoized technique, [...]]]></description>
			<content:encoded><![CDATA[<p>Fibonacci number can be calculated using different techniques. Most common recursive technique is brute force where we calculate Fibonacci number by repeatedly calling the method of numbers less by one and less by two. In this technique, we do not care the fact the previous Fibonacci number may have been calculated. However, in Memoized technique, we store the results in some array or variable and calculate new values from old calculated values. Source codes for brute force technique, memorized with array and memoized with one variable are given below.</p>
<p>1) Source code for<strong> Brute force technique</strong></p>
<p>public class Fibonacci { </p>
<p>&#160;&#160;&#160; int recursiveCalls= 0;   <br />&#160;&#160;&#160; </p>
<p>&#160;&#160;&#160; public int fib(int n){   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; recursiveCalls++;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if( n==0 || n==1){    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return 1;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; else{    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return fib(n-2) + fib(n-1);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; public static void main(String[] args) {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; Fibonacci f = new Fibonacci();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(&quot;Fib (30): &quot; + f.fib(30));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(&quot;Counter: &quot; + f.recursiveCalls);    <br />&#160;&#160;&#160; }    <br />}</p>
<p>2) Source code for <strong>Memoized technique using Table Array</strong></p>
<p>public class Fibonacci {   <br />&#160;&#160;&#160; int recursiveCalls= 0 ;    <br />&#160;&#160;&#160; int memoizedFib(int n){&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; int f[]= new int[n+1];    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return recursiveFib(f,n);    <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; int recursiveFib(int f[], int n){    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; recursiveCalls++;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ( n==0 || n==1)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; f[n] = 1;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; else{    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (f[n-1] == 0)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; recursiveFib(f, n-1);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (f[n-2] == 0)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; recursiveFib(f, n-2);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; f[n] = f[n-2] + f[n-1];    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return f[n];    <br />&#160;&#160;&#160; }    <br />}</p>
<p>&#160;</p>
<p>3) Source code for <strong>Memoized technique using one variable</strong></p>
<p>public class Fibonacci { </p>
<p>&#160;&#160;&#160; int recursiveCalls = 0;   <br />&#160;&#160;&#160; int a=0;    <br />&#160;&#160;&#160; int fib(int n){    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; recursiveCalls ++;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (n==1){    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; a = 1;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return 0 + a;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; else{    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int b = fib(n-1);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int c = a + b;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; a = b;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return c;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160; }    <br /> }</p>
<p>&#160;</p>
<p>The number of recursive Calls made for calculation of Fibonacci of 30 is 2692537 in Brute force technique where as it is 30 in memoized techniques</p>
]]></content:encoded>
			<wfw:commentRss>http://www.surajshrestha.com.np/algorithm/memoized-version-of-fibonacci-number-calculation-using-only-one-variable.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
