How do I think about recursion in programming?

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Programming Recursion
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 2K views
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
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)
 
Reply
  • Like
Likes   Reactions: 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.
 
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.