Recursive functions for beginners?

I am looking for some examples of recursive functions, preferably those that show an increase in complexity. I understand the basic recursive functions, but I am unable to implement them in my code. I have never used them in my code before, and I know that this does not always cause it, but I would like to try it. Is there a good resource with examples and maybe problems of some kind? Or even some simple math problems (Project Euler-style?) That I could use recursion for?

For examples, I prefer C #, but something works.

+4
source share
3 answers

1 the simplest are factorials, Fibonacci sequences, or any mathematical sequence defined by recursive functions.

2, then you can go to

any algorithms that use the first depth search.

eg. tree traversal, graph traversal ,

problems with the search, for example problems with 8 kings .

3 you would probably like to learn the separation and subjugation algorithm, for example. merge sort and quick sort. they are usually implemented recursively.

They are all very classic!

+3
source

I found the online courses provided by Stanford Engineering Everywhere, an excellent resource. If you look at the CS106B course, lectures 7 through 11, you should have a good understanding of recursion. They also provide exercises to solve problems.

http://see.stanford.edu/see/lecturelist.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e

+2
source

Well, if you are having trouble implementing the basic recursive functions, I would suggest that you really don't understand them. But there is no shame in this, not everyone "gets" recursion easily. Instead of leaning your head against simple algorithms, I suggest you try:

  • Thinking of exploring the use of recursive data structures such as trees, graphs, or lists. Learning to use recursion in situations where recursion is a natural approach is probably a better way to recursive joy than trying many examples that need to be solved with loops.
  • Using a language in which recursion is a basic, primitive, concept. Haskell and some other functional programming languages. Often the most natural way to solve the problem with FP is to implement recursion; in other languages, you often have to struggle first with the weird approach to recursion. Does C # have a weird approach to recursion, I don't know.
0
source

Source: https://habr.com/ru/post/1306682/


All Articles