logo
down
shadow

Defining a recursive function as iterative?


Defining a recursive function as iterative?

By : Eyal
Date : November 17 2020, 11:58 AM
hope this fix your issue In the book, authors have an example of formulating an iterative process for computing the Fibonacci numbers.
code :
(define (fib n)
  (fib-iter 1 0 n))

(define (fib-iter a b count)
  (if (= count 0)
    b
    (fib-iter (+ a b) a (- count 1))))
;; an interative process implementation                                                                                                                       
(define (f-i n)                                                                                                                                               
  ;; f2 is f(n+2), f1 is f(n+1), f0 is f(n)                                                                                                                   
  (define (interative-f f2 f1 f0 count)                                                                                                                       
    (cond                                                                                                                                                     
      ((= count 0) f0)                                                                                                                                         
      (else (interative-f                                                                                                                                      
              (+ f2 (* f1 2) (* f0 3))                                                                                                                          
              f2                                                                                                                                                
              f1                                                                                                                                                
              (- count 1)))))                                                                                                                                   
  (interative-f 2 1 0 n))             


Share : facebook icon twitter icon
Converting an iterative function to recursive

Converting an iterative function to recursive


By : kc64
Date : March 29 2020, 07:55 AM
it helps some times I think this should work (but note that it requires an extra argument for tracking start):
code :
int count(node *start)
{
    return count_helper(start, start);
}
int count_helper(node *current, node *start)
{
    int c;
    c = 0;
    if(current == NULL)
        return 0;
    if((current->roll_no) == 20)
        c = 1;
    if(current->next == start) return c;
    return (c + count_helper(current->next, start));
}
From Recursive To Iterative Function

From Recursive To Iterative Function


By : user2978660
Date : March 29 2020, 07:55 AM
Any of those help I am trying to make from f_rec (recursive function) to f_iter (iterative function) but I can't. (My logic was to create a loop to calculate the results of f_rec(n-1). , The following is the idea
code :
  int first=1,second=1,third=1; /* if n<=3 then the respective is the answer */
  for(i=4;i<=n;i++)
  {
     int next=first+2*second+third;
     first=second;
     second=third;
     third=next;
  }
  cout<<"The answer is "<<next<<endl;
    int f_rec(int n)
    {
        if(n>=3)
        {
           if(F[n]!=-1)return F[n];

           F[n]=f_rec(n-1)+2*f_rec(n-2)+f_rec(n-3);
           return F[n];       
        }  

        else 
           return 1;
Recursive function to iterative

Recursive function to iterative


By : MR_stat
Date : March 29 2020, 07:55 AM
will be helpful for those in need To calculate T(n, k) you can use the following iterative approach:
S(i) represents the vector with T(0,i) to T(n, i) Initialize your status vector S(0) with zeros For i=1 to k Calculate S(i) using S(i-1) Endfor Your result is the last element of S(i)
C : Recursive Function to Iterative

C : Recursive Function to Iterative


By : user2340848
Date : March 29 2020, 07:55 AM
it fixes the issue I just happened to recognize the pattern, so I have no analytical method to show how to transform the original code to this solution. I just knew it:
code :
static void Fill(size_t n, char (*A)[n])
{
    for (size_t i = 0; i < n; ++i)
    for (size_t j = 0; j < n; ++j)
        A[i][j] = j == 0 || i != 0 && A[i-1][j-1] != A[i-1][j] ? 'D' : ' ';
}
How to convert this recursive function to an iterative one?

How to convert this recursive function to an iterative one?


By : user3607889
Date : March 29 2020, 07:55 AM
around this issue I think I may have figured it out, but I would like a second opinion before I say it is all good.
The logic here is each time recursion would be used, push the current state of the loop to the stack along with the state that can answer whether or not the previously stacked state is valid. If the answer to the question is true, the entire stack is unwound and the method terminates, otherwise, continue searching.
code :
    public readonly struct State
    {
        public State(int x, int y = 0)
        {
            X = x;
            Y = y;
        }

        public int X { get; }

        public int Y { get; }
    }

    public static void BipartiteMatch(int x, int n, bool[] seen, int[] matches, Func<int, int, bool> isMapped)
    {
        var stack = new Stack<State>(new[] {new State(x)});

        while (stack.TryPop(out var state))
        {
            for (var y = state.Y; y < n; y++)
            {
                if (seen[y] || !isMapped(state.X, y)) continue;

                seen[y] = true;

                if (matches[y] >= 0)
                {
                    stack.Push(new State(state.X, y));
                    stack.Push(new State(matches[y]));
                    break;
                }

                matches[y] = state.X;

                while (stack.TryPop(out state))
                {
                    matches[state.Y] = state.X;
                }

                break;
            }
        }
    }
Related Posts Related Posts :
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org