Click here to Skip to main content
14,868,681 members
Home / Discussions / C#
   

C#

 
GeneralRe: Hacking Pin
Mycroft Holmes1-Mar-21 11:02
professionalMycroft Holmes1-Mar-21 11:02 
GeneralRe: Hacking Pin
Pete O'Hanlon3-Mar-21 2:03
mvePete O'Hanlon3-Mar-21 2:03 
GeneralRe: Hacking Pin
Eddy Vluggen2-Mar-21 9:56
professionalEddy Vluggen2-Mar-21 9:56 
GeneralRe: Hacking Pin
Gerry Schmitz2-Mar-21 18:29
mveGerry Schmitz2-Mar-21 18:29 
QuestionKnight's tour, recursion, dynamic programming, optimization Pin
Kari Trust28-Feb-21 14:30
MemberKari Trust28-Feb-21 14:30 
AnswerRe: Knight's tour, recursion, dynamic programming, optimization Pin
trønderen28-Feb-21 15:59
Membertrønderen28-Feb-21 15:59 
GeneralRe: Knight's tour, recursion, dynamic programming, optimization Pin
Kari Trust2-Mar-21 2:04
MemberKari Trust2-Mar-21 2:04 
GeneralRe: Knight's tour, recursion, dynamic programming, optimization Pin
trønderen2-Mar-21 8:53
Membertrønderen2-Mar-21 8:53 
Kari Trust wrote:
I watched a lot of tutorials - on recursion and almost everyone has an example with Fibonacci
I can see a single reason for using fibonacci to illustrate recursion: To show that it isn't needed. The keyword is tail recursion.

Certainly, a good compiler will detect tail recursion and save stack space and call overhead, generating a simple jump to the top of the function (after fixing up the parameter block, of course), changing the recursion to an iteration. You might as well do that yourself Smile | :) ... unless, of course, you are so deeply into recursive thinking that you find it hard to consider fibonacci anything but recursive. (I can imagine mathematicians that are that way, but not many programmers!

To me, the obvious way to write a fibonacci-function would be (I consider the sequence to start with 0, labeled the 0th fibonacci number - some people argue details):
static int fib(int n) {
  if (n < 2) return n;
  int prev = 1;
  int fibn = 1;

  for (int iteration = 2; iteration < n; iteration++) {
    int fibnext = fibn + prev;
    prev = fibn;
    fibn = fibnext;
  }
  return fibn;
}
Bonus sidetrack (or is it?):

If you want to make sure that you master recursion, terminating conditions in particular, try this little exercise: Make a console program with recursive function that for each call writes an output line on the console of RecursionDepth number of spaces, followed by an asterisk (and a newline).

If the main program calls this function with arguments NumberOfTeeth, BladeWidth, ToothLength, the output should look like a sawblade; the recursion goes to the maximum recursion depth, ToothLength. Then it returns to BladeWidth recursion depth, before it again recurses to ToothLength depth a second time, and repeats this for at total of NumberOfTeeth dives into the maximum depth, before finally returning to the main program.

If you are a seasoned recursioner, maybe you will find this problem trivial (in that case, forward it to your students or junior programmers!) I have never ever seen anyone getting this perfectly right on the first try, though! Most programmers give it a first try, then they curse before making the first correction, then they re-curse before making a second correction, ... The cursing often goes to the maximum depth of re-cursing before they get it right Smile | :)

modified 2-Mar-21 15:00pm.

QuestionApplication crashes suddenly without any message [Solved] Pin
Alex Dunlop26-Feb-21 22:05
MemberAlex Dunlop26-Feb-21 22:05 
AnswerRe: Application crashes suddenly without any message Pin
BillWoodruff26-Feb-21 22:23
mveBillWoodruff26-Feb-21 22:23 
GeneralRe: Application crashes suddenly without any message Pin
Alex Dunlop26-Feb-21 22:29
MemberAlex Dunlop26-Feb-21 22:29 
GeneralRe: Application crashes suddenly without any message Pin
OriginalGriff26-Feb-21 22:35
mveOriginalGriff26-Feb-21 22:35 
AnswerRe: Application crashes suddenly without any message Pin
OriginalGriff26-Feb-21 22:32
mveOriginalGriff26-Feb-21 22:32 
GeneralRe: Application crashes suddenly without any message Pin
Alex Dunlop26-Feb-21 23:01
MemberAlex Dunlop26-Feb-21 23:01 
QuestionHow to reset TextBoxes at once? [Solved] Pin
Alex Dunlop25-Feb-21 7:06
MemberAlex Dunlop25-Feb-21 7:06 
AnswerRe: How to reset TextBoxes at once? Pin
Gerry Schmitz25-Feb-21 8:42
mveGerry Schmitz25-Feb-21 8:42 
GeneralRe: How to reset TextBoxes at once? Pin
OriginalGriff25-Feb-21 8:51
mveOriginalGriff25-Feb-21 8:51 
GeneralRe: How to reset TextBoxes at once? Pin
Gerry Schmitz25-Feb-21 9:17
mveGerry Schmitz25-Feb-21 9:17 
AnswerRe: How to reset TextBoxes at once? Pin
OriginalGriff25-Feb-21 8:50
mveOriginalGriff25-Feb-21 8:50 
GeneralRe: How to reset TextBoxes at once? Pin
Alex Dunlop25-Feb-21 19:10
MemberAlex Dunlop25-Feb-21 19:10 
GeneralRe: How to reset TextBoxes at once? Pin
Dave Kreskowiak25-Feb-21 19:44
mveDave Kreskowiak25-Feb-21 19:44 
GeneralRe: How to reset TextBoxes at once? PinPopular
OriginalGriff25-Feb-21 19:59
mveOriginalGriff25-Feb-21 19:59 
GeneralRe: How to reset TextBoxes at once? Pin
Eddy Vluggen26-Feb-21 6:39
professionalEddy Vluggen26-Feb-21 6:39 
GeneralRe: How to reset TextBoxes at once? Pin
BillWoodruff25-Feb-21 21:43
mveBillWoodruff25-Feb-21 21:43 
AnswerRe: How to reset TextBoxes at once? Pin
Ralf Meier26-Feb-21 3:05
professionalRalf Meier26-Feb-21 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.