How do I think about recursion in programming?

In summary, the author got the idea for problems like factorials or finding sum from 1 to n, where there's a pattern visible. Like this:-For things like addition, subtraction, division, multiplication of two numbers as well, the author got the idea from looking at problems with a pattern visible.-Now, the author is wondering how it'll be for finding a palindrome of a number. The author has a solution, but is not understanding how we came towards it. The author is looking for how to come towards a solution than a solution itself.
  • #1
shivajikobardan
674
54
Homework Statement
thinking about recursion in programming for palindrome checker
Relevant Equations
none
I got the idea for problems like factorials or finding sum from 1 to n, where there's a pattern visible. Like this:
n7yRPLDzxUujdzMyPic6CcpXm0o6GEvJZ1lmZzfTfbkhaa608Q.png

Source:https://99x.io/blog/recursion-is-not-hard-here-is-the-right-way-to-think

I got it for things like addition, subtraction, division, multiplication of two numbers as well.

eg: a+b

=a+b-1+1

=SUM(a+1,b-1)

Now, I am wondering how it'll be for finding a palindrome of a number? I've a solution but I'm not understanding how we came towards it. I'm looking for how to come towards a solution than a solution itself.

A number is palindrome if it's same as it's reversed. eg: REFER, RADAR, NOON etc are palindrome strings.

I'm unable to grasp a pattern here. Also, please share some books based on mathematics(Not full books but part of book on recursion) that have exercises of recursive functions. I think that'd help greatly with recursion.
 
Physics news on Phys.org
  • #2
If the two ends are equal then remove them and call the function with the smaller string.
 
  • Like
Likes scottdave
  • #3
You don't necessarily have to see a pattern to use recursion. As long as the recursive call is applied to a smaller problem of the same nature, you can use recursion to repetitively reduce it to an elementary problem.
 
  • #4
which college/university level math book could contain excerpts about recursive functions and practice problems?
 
  • #5
shivajikobardan said:
which college/university level math book could contain excerpts about recursive functions and practice problems?
Recursion in programming is associated with the mathematical concept of sequences but you won't find computing problems of this kind in a maths text book.

The Wikipedia article on this is quite a good start point https://en.wikipedia.org/wiki/Recursion_(computer_science)
 
  • Like
Likes scottdave and FactChecker
  • #6
pbuk said:
Recursion in programming is associated with the mathematical concept of sequences but you won't find computing problems of this kind in a maths text book.

The Wikipedia article on this is quite a good start point https://en.wikipedia.org/wiki/Recursion_(computer_science)
that's like entire dsa(recursion related) in 1 post.
 
  • #7
shivajikobardan said:
Now, I am wondering how it'll be for finding a palindrome of a number? I've a solution but I'm not understanding how we came towards it. I'm looking for how to come towards a solution than a solution itself.
It might help if you show the solution that you have questions about.
 
  • #9
In that link, this is the critical part that hints at a solution using regression:
7. If the first and last characters of the string are the same then carry out the same for substring with the first and last character removed.

That means that the problem can be simplified to a smaller string to check for being a palindrome. Doing that over and over until the string is so short that it can be checked trivially.
 
  • #10
I had not heard of StudyTonight site before. Can anybody comment on it for learning (for example JavaScript)?
 
  • #11
scottdave said:
Can anybody comment on it for learning (for example JavaScript)?
It seems completely useless for learning JavaScript. Stick to well known resources like Programiz.
 
  • #12
pbuk said:
It seems completely useless for learning JavaScript. Stick to well known resources like Programiz.
Thanks
 

FAQ: How do I think about recursion in programming?

1. What is recursion in programming?

Recursion in programming is a technique where a function calls itself repeatedly until a certain condition is met. It is a useful approach for solving problems that can be broken down into smaller, similar subproblems.

2. How does recursion work?

In recursion, a function calls itself with a smaller input until it reaches a base case, which is a condition that stops the function from calling itself again. The function then returns the result of the base case to the previous call, which in turn returns its result to the previous call, and so on until the original call is completed.

3. What are the advantages of using recursion?

Recursion can simplify the code and make it more readable, especially for problems that involve repetitive tasks. It also allows for the use of elegant and concise solutions to complex problems. Additionally, some problems are naturally recursive in nature, making recursion the most efficient approach to solving them.

4. What are the potential drawbacks of recursion?

One potential drawback of recursion is that it can be memory-intensive, as each function call adds a new layer to the call stack. This can lead to stack overflow errors if the recursion is not properly managed. Additionally, recursive solutions may not be as efficient as iterative solutions for some problems.

5. How can I become better at thinking about recursion in programming?

The best way to become better at thinking about recursion is to practice. Start with simple problems and gradually move on to more complex ones. It is also helpful to understand the concept of recursion and how it works, as well as being familiar with common recursive patterns and techniques. Reading and studying code examples of recursive solutions can also improve your understanding and proficiency in using recursion in programming.

Similar threads

Replies
2
Views
2K
Replies
10
Views
2K
Replies
17
Views
1K
Replies
4
Views
1K
Replies
15
Views
2K
Replies
8
Views
1K
Replies
1
Views
892
Back
Top