Jump to content

Recursive method C#

lubblig

I would like some simple recursion examples to get some inspiration from. I've already looked at factorials but I'd like some more simple examples to work with.

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Look at some implementations for linked lists, they often use recursion for navigating through the list.

Link to comment
Share on other sites

Link to post
Share on other sites

Get the nth Fibonacci number

 

Get the ____ in a data structure (array, list, tree, etc)

  • Where _____ can be sum, product, number of items, max value, etc

 

Implement a basic mergesort and quicksort

 

Find the number of correct paths in a maze

 

Solve a Sudoku puzzle

 

Any problem in the dynamic programming section of challenge sites.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, madknight3 said:

Get the nth Fibonacci number

 

Get the ____ in a data structure (array, list, tree, etc)

  • Where _____ can be sum, product, number of items, max value, etc

 

Implement a basic mergesort and quicksort

 

Find the number of correct paths in a maze

 

Solve a Sudoku puzzle

 

Any problem in the dynamic programming section of challenge sites.

I did a Fibonacci method and made it work, although, what's the point of recursive methods? Could you not simply use a while loop or similar?

The reason I got interested is that I deleted some files off of an FTP in filezilla and it said that it was removing the files recursively and I got interested of what recursion actually was. But again, is there really any benefit of doing something like Fibonacci numbers or Factorials in a recursive method over a simple loop?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, lubblig said:

I would like some simple recursion examples to get some inspiration from. I've already looked at factorials but I'd like some more simple examples to work with.

Recursion is very useful for complex algorithhms.  The applications you listed are simple tasks that could be accomplished by a simple while or for loop.  recursion is very useful for implementing multiple conditional statements to search through large amounts of data.

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, lubblig said:

I did a Fibonacci method and made it work, although, what's the point of recursive methods? Could you not simply use a while loop or similar?

The reason I got interested is that I deleted some files off of an FTP in filezilla and it said that it was removing the files recursively and I got interested of what recursion actually was. But again, is there really any benefit of doing something like Fibonacci numbers or Factorials in a recursive method over a simple loop?

All recursive algorithms can be written iteratively and all iterative algorithms can be written recursively. The point of the simple examples like Fibonacci, factorials, etc isn't to tell you that doing them recursively is better, it's just to give you practice in doing things recursively. Once you know how to write things recursively, then you add another technique to your skill set. You can then choose to use which ever technique makes the most sense for the task at hand. Sometimes one way is easier than the other.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×