<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title>Topic &#x27;Re: Please help me understand this function&#x27; in forum &#x27;Webmasters and Developers&#x27; - dslreports.com</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27297627</link>
<description></description>
<language>en</language>
<pubDate>Wed, 22 May 2013 05:15:18 EDT</pubDate>
<lastBuildDate>Wed, 22 May 2013 05:15:18 EDT</lastBuildDate>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27302035</link>
<description><![CDATA[cdru posted : <div class="bquote"><said>said by <a href="/profile/156437" onClick="this.blur(); return popup(event,'/uidpop?ajh=1&uid=156437');">dave</a>:</said><p>I would assume the whole point of the OP's code is that it's an exercise in understanding recursive activation, since (as you say) it's not a sensible approach to use otherwise.<br> </p></div>I would presume that too.  But its one of the classic problems that are tackled multiple different ways showing that there's more then one method to tackle a problem, and that there are advantages and disadvantages for each method.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27302035</guid>
<pubDate>Thu, 05 Jul 2012 21:03:09 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27301967</link>
<description><![CDATA[dave posted : I would assume the whole point of the OP's code is that it's an exercise in understanding recursive activation, since (as you say) it's not a sensible approach to use otherwise.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27301967</guid>
<pubDate>Thu, 05 Jul 2012 20:35:10 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27301721</link>
<description><![CDATA[cdru posted : <div class="bquote"><said>said by cplusnoob :</said><p>I'll do some more research I guess.  I have caught on really quick to everything up to this point and I just want to make sure I understand what the code is doing and not just copy / paste it.</p></div>There's 3 common ways (maybe more) that actually solve the calculation.  Each one has their advantages and disadvantages.<br><br>The simplest/quickest algorithm to implement is the recursive algorithm that you're working with now.  It's also the least efficient.  The same function is called multiple times with the same value, and the extensive pushing and popping to the call stack kills the performance.  <br><br>An improved algorithm instead of recursively starting at n and working there way down to 1.  Instead it performs a definite loop from 1 to n, adding as it goes.  It's still has to iterate over all n levels, but saves considerably due to the stack not growing very rapidly.<br><pre class="brush: text">static int fib(int n)&#012;{&#012;    int u = 0;&#012;    int v = 1;&#012;    int i, t;&#012; &#012;    for (i = 2; i &lt;= n; i++)&#012;    {&#012;        t = u + v;&#012;        u = v;&#012;        v = t;&#012;    }&#012; &#012;    return v;&#012;}&#012; &#012;</pre><!--end code block--><br>The fastest performance is just O(1), and reduces the computation to a single line of code.  <br><pre class="brush: text">static int fib(int n)&#012;{&#012;    return (int)((1 / Math.Sqrt(5)) * (Math.Pow(((1 + Math.Sqrt(5)) / 2), n) - Math.Pow(((1 - Math.Sqrt(5)) / 2), n)));&#012;}&#012; &#012;</pre><!--end code block--><br>Calling the function recursively, for n = 1 to 50, run time was 946708 milliseconds. (over 15 minutes).  Running for the same sample size, the other two functions did not even register 1 ms.  I had to up the sample size to 1 to 10000 where looping was 274 ms and the single line function was 53 ms.<br>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27301721</guid>
<pubDate>Thu, 05 Jul 2012 19:16:13 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27300259</link>
<description><![CDATA[Rob posted : <div class="bquote"><said>said by cplusnoob :</said><p>LOL that's actually the book I am using right now Rob!<br> </p></div>LOL! ]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27300259</guid>
<pubDate>Thu, 05 Jul 2012 12:02:49 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299857</link>
<description><![CDATA[cdru posted : <div class="bquote"><said>said by cplusnoob :</said><p>cdru, I understand the purpose of the function, I just don't understand how it actually works to achieve the result.  It's calling itself over and over with one less number each time until the number is  2.  How are those answers being returned as the correct answer if the only way to break the cycle is by having the variable be  2?</p></div>It's calling the function recursively.<br><br>For values of 0 and 1, it sounds like you understand.  If 2 is initially passed in, before it can return the value it calls the same function with n-1 (2-1 = 1) and n-2 (2-2=0).  Those two calls return 1 and 0 respectively.  It adds those up and returns the sum, 1.<br><br>For a more complicated example, pass in 6.  That would make recursive calls to n=5 and n=4.  Then 4, 3; 3, 2.  Then 3, 2; 2, 1; 2, 1; 1, 0.  Then 2, 1; 1, 0; 1, 0.  Then 1, 0.  Each call adding up the returned value then finally returning the final value to display.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299857</guid>
<pubDate>Thu, 05 Jul 2012 10:15:37 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299503</link>
<description><![CDATA[anon posted : LOL that's actually the book I am using right now Rob!]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299503</guid>
<pubDate>Thu, 05 Jul 2012 07:45:13 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299502</link>
<description><![CDATA[anon posted : Well that's odd.  I did post the whole thing and I previewed and it all worked fined.  I guess maybe it was trimmed when it got posted?<br><br>I'll take a look at your link Rob, I am reading a book now and so far it has been great but it doesn't explain how it works for some reason, just the output.  Everything else up to this point has been explained very well.<br><br>cdru, I understand the purpose of the function, I just don't understand how it actually works to achieve the result.  It's calling itself over and over with one less number each time until the number is  2.  How are those answers being returned as the correct answer if the only way to break the cycle is by having the variable be  2?<br><br>I'll do some more research I guess.  I have caught on really quick to everything up to this point and I just want to make sure I understand what the code is doing and not just copy / paste it.<br><br>Thanks again for the input.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299502</guid>
<pubDate>Thu, 05 Jul 2012 07:36:41 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299168</link>
<description><![CDATA[cdru posted : You're code was mangled.  Be sure to place all your code between [code ] [/code ] brackets, minus the spaces.<br><br>The function that you look like you will have will compute a Fibonacci number.  It's value is given by F(n) = F(n-1) + F(n-2) with seed values F(0) = 0 and F(1) = 1. F(n-1) and F(n-2) are recursively called and then the sums are added up.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27299168</guid>
<pubDate>Wed, 04 Jul 2012 23:21:58 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27298746</link>
<description><![CDATA[Rob posted : Not sure as I don't deal with C++, but would this help shed some light?<br><br>&raquo;<A HREF="http://books.google.com/books?id=JUT-rDb5R1UC&pg=PT215&lpg=PT215&dq=int+GetFibNumber%28int+FibIndex%29&source=bl&ots=6UJkfdkqxC&sig=u2TYGK2DVKMbNE5EY542uIBGKdY&hl=en&sa=X&ei=aNX0T8vUHoP28gSntpzGBg&ved=0CCUQ6AEwAA#v=onepage&q=int%20GetFibNumber%28int%20FibIndex%29&f=false" >books.google.com/books?id=JUT-rD&middot;&middot;&middot;&f=false</A><br><small>--<br><A HREF="http://www.checksite.us"> CheckSite.us </a> | <A HREF="http://www.yourip.us"> YourIP.us </a>|<A HREF="http://www.reverseip.us"> Reverseip.us </a></small>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27298746</guid>
<pubDate>Wed, 04 Jul 2012 19:45:28 EDT</pubDate>
</item>

<item>
<title>Re: Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27298317</link>
<description><![CDATA[stray posted : Can't for the life of me figure out why this works - missing closing parenthesis, "}", empty includes, and no main().<br><br>Perhaps you could post the entire program??<br><small>--<br><A HREF="http://www.vrtifacts.com">V-Rtifacts</a><b>&nbsp;-&nbsp;When Virtual Reality Was More Than Virtual</b></small>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-Please-help-me-understand-this-function-27298317</guid>
<pubDate>Wed, 04 Jul 2012 16:24:00 EDT</pubDate>
</item>

<item>
<title>Please help me understand this function</title>
<link>http://www.dslreports.com/forum/Please-help-me-understand-this-function-27297627</link>
<description><![CDATA[anon posted : Hey all,<br><br>Not sure if this is the right forum for this but it seemed appropriate, I apologize in advance if there is a better forum for this sort of topic.<br><br>I know it's the 4th and this probably won't be seen today but going to put it out there anyway.<br><br>I cannot, for the life of me, figure out why this works:<br><br>[code=C++]<br>#include<br>#include<br><br>using namespace std;<br><br>int GetFibNumber(int FibIndex)<br>{<br>&#9;if (FibIndex > Index;<br><br>&#9;cout<br>return GetFibNumber(FibIndex-1) + GetFibNumber(FibIndex-2);<br></code><br><br>This is a recursive function, so it keeps calling itself but it's not doing this:<br><br>6 - 1 = 5<br>6 - 2 = 4<br>5 + 4 = 9<br><br>It's doing:<br><br>6 - 1 = 5<br>5 - 2 = 3<br>5 + 3 = 8<br><br>I guess I don't understand how it is calling the function twice in the same line and getting a different number (I would think it would need to be two separate calls) plus how the function is exiting the loop when both numbers are still > 2.<br><br>Hopefully this makes sense and it's probably something easy I'm overlooking but it's just not clicking for me.<br><br>Any insight would be appreciated, TIA!]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Please-help-me-understand-this-function-27297627</guid>
<pubDate>Wed, 04 Jul 2012 11:52:20 EDT</pubDate>
</item>

</channel>
</rss>
