<?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>Talk Binary &#187; log recursive function</title>
	<atom:link href="http://talkbinary.com/tag/log-recursive-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C++ Recursion &#8211; Logarithm</title>
		<link>http://talkbinary.com/programming/c/c-recursion-logarithm/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-logarithm/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 08:25:08 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[log base 2]]></category>
		<category><![CDATA[log base n]]></category>
		<category><![CDATA[log function]]></category>
		<category><![CDATA[log recursive function]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[recursive function]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2397</guid>
		<description><![CDATA[Finding the logarithm of a number given a base Below is an example of how to determine the logarithm of a number of base 2 and then of any base. The logarithm of a number using a given base is power to which the base must be raised to result in the number. This tutorial<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-logarithm/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h2 id="section-1">Finding the logarithm of a number given a base</h2>
<p>Below is an example of how to determine the logarithm of a number of base 2 and then of any base. The logarithm of a number using a given base is power to which the base must be raised to result in the number. This tutorial assumes you understand <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">C++ Recursion</a> and the <a href="http://talkbinary.com/programming/c/c-recursion-power/">C++ Recursion &#8211; Power</a> function.</p>
<blockquote>
<h3 id="section-2">Example of finding the logarithm given a base</h3>
<p>You could find the base using two different methods.</p>
<ol>
<li>Multiply the base by itself until you receive the number.</li>
<li>Divide the number by the base, and repeat until you result in 1. For this method the result is the number of computations to get to 1.</li>
</ol>
<p>[pmath]logBase2(8) = 3<br />
2 * 2 * 2 = 8</p>
<p>Method 2:<br />
8/2 = 4<br />
4/2 = 2<br />
2/2 = 1</p>
<p>logBaseN(16,2) = 4<br />
2 * 2 * 2 * 2 = 16</p>
<p>Method 2:<br />
16/2 = 8<br />
8/2 = 4<br />
4/2 = 2<br />
2/2 = 1<br />
[/pmath]<br />
<br/>
</p></blockquote>
<h2 id="section-3">Recursive version of logBase2 function</h2>
<p>Given the example above, could you come up with a logBase2 recursive function?  Let&#8217;s try using the 2nd method described above. So what would be our base case and header?<br />
<span id="more-2397"></span></p>
<blockquote>
<h3 id="section-4">Header and base case for logBase2 function</h3>
<p>Below is the header for the logBase2 function. We only need one parameter. </p>
<pre lang="c++">int logBase2(int num) { </pre>
<p><br/>What about our base case? By using our second method we want to stop dividing our number by our base 2 until we result in 1.</p>
<pre lang="c++">int logBase2(int num) { 

      if ( num == 1 )
           return 0; </pre>
</blockquote>
<blockquote>
<h3 id="section-5">LogBase2 Recursive Function</h3>
<p>Looking back at our second method, we count the number of computations until our number is 1. Let&#8217;s do this. We also break down the problem by diving our number by 2 at each iteration.</p>
<pre lang="c++">int logBase2(int num) { 

      if ( num == 1 )
           return 0; 

      return 1 + logBase2(num/2);

}</pre>
<p><br/>Yes, that&#8217;s it. This recursive function will return the number which is the power to the base to get to our number.
</p></blockquote>
<h2 id="section-6">Recursive version of logBaseN function</h2>
<p>Our previous function took care of logBase2 values. What about logBaseN values? This simply means you pass in the base as well. This addition should be trivial.</p>
<blockquote>
<h3 id="section-5">LogBase2 Recursive Function</h3>
<p>Now, we are simply modifying our previous function but this time around we are also passing in the base.</p>
<pre lang="c++">int logBaseN(int num, int base) { 

      if ( num == 1 )
           return 0; 

      return 1 + logBase2(num/base, base);

}</pre>
<p><br/>
</p></blockquote>
<h2 id="section-8">Going further</h2>
<ol>
<li>1. Can you take care of error detection? What if a user puts in erroneous numbers or bases?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-logarithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-05-23 23:35:51 -->
