Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:25:18+00:00 2026-05-31T22:25:18+00:00

I’ve heard that any recursive algorithm can always be expressed by using a stack.

  • 0

I’ve heard that any recursive algorithm can always be expressed by using a stack. Recently, I’ve been working on programs in an environment with a prohibitively small available call stack size.

I need to do some deep recursion, so I was wondering how you could rework any recursive algorithm to use an explicit stack.

For example, let’s suppose I have a recursive function like this

function f(n, i) {
  if n <= i return n
  if n % i = 0 return f(n / i, i)
  return f(n, i + 1)
}

how could I write it with a stack instead? Is there a simple process I can follow to convert any recursive function into a stack-based one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:25:19+00:00Added an answer on May 31, 2026 at 10:25 pm

    If you understand how a function call affects the process stack, you can understand how to do it yourself.

    When you call a function, some data are written on the stack including the arguments. The function reads these arguments, does whatever with them and places the result on the stack. You can do the exact same thing. Your example in particular doesn’t need a stack so if I convert that to one that uses stack it may look a bit silly, so I’m going to give you the fibonacci example:

    fib(n)
        if n < 2 return n
        return fib(n-1) + fib(n-2)
    
    function fib(n, i)
        stack.empty()
        stack.push(<is_arg, n>)
        while (!stack.size() > 2 || stack.top().is_arg)
            <isarg, argn> = stack.pop()
            if (isarg)
                if (argn < 2)
                    stack.push(<is_result, argn>)
                else
                    stack.push(<is_arg, argn-1>)
                    stack.push(<is_arg, argn-2>)
            else
                <isarg_prev, argn_prev> = stack.pop()
                if (isarg_prev)
                    stack.push(<is_result, argn>)
                    stack.push(<is_arg, argn_prev>)
                else
                    stack.push(<is_result, argn+argn_prev>)
         return stack.top().argn
    

    Explanation: every time you take an item from the stack, you need to check whether it needs to be expanded or not. If so, push appropriate arguments on the stack, if not, let it merge with previous results. In the case of fibonacci, once fib(n-2) is computed (and is available at top of stack), n-1 is retrieved (one after top of stack), result of fib(n-2) is pushed under it, and then fib(n-1) is expanded and computed. If the top two elements of the stack were both results, of course, you just add them and push to stack.

    If you’d like to see how your own function would look like, here it is:

    function f(n, i)
        stack.empty()
        stack.push(n)
        stack.push(i)
        while (!stack.is_empty())
            argi = stack.pop()
            argn = stack.pop()
            if argn <= argi
                result = argn
            else if n % i = 0
                stack.push(n / i)
                stack.push(i)
            else
                stack.push(n)
                stack.push(i + 1)
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.