Comp Sci How do I think about recursion in programming?

AI Thread Summary
Recursion in programming can be understood through patterns, as seen in problems like calculating factorials or summing numbers. To determine if a number is a palindrome, one can simplify the problem by checking if the first and last characters are equal, then recursively applying this to the substring. Understanding recursion does not always require identifying a clear pattern; the key is to reduce the problem to a smaller instance of itself. Resources for learning recursion include Wikipedia and various programming sites, though some users suggest sticking to more reputable sources for specific languages like JavaScript. Overall, grasping the concept of recursion is essential for solving complex programming challenges.
shivajikobardan
Messages
637
Reaction score
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
If the two ends are equal then remove them and call the function with the smaller string.
 
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.
 
which college/university level math book could contain excerpts about recursive functions and practice problems?
 
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
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.
 
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.
 
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
 

Similar threads

Replies
2
Views
2K
Replies
10
Views
2K
Replies
17
Views
3K
Replies
4
Views
1K
Replies
15
Views
2K
Replies
2
Views
1K
Back
Top