2014 dxdy logo

Научный форум dxdy

Математика, Физика, Computer Science, Machine Learning, LaTeX, Механика и Техника, Химия,
Биология и Медицина, Экономика и Финансовая Математика, Гуманитарные науки




 
 It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 19:56 
I have the job of verifying and if it is possible to calculate the catalan number by means of the tail recursion, I could do the calculation with the stack recursion using the definition, but I could not do it by means of the tail recursion

Код:
int catalan(int n){
    if(n==0){
        return 1;
    }
    else{
        return 2*(2*n-1)*Catalan(n-1)/(n+1);
    }
}

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:03 
sivaprasad222
Catalan(n-1) на catalan(n-1) менять не пробовали?

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:07 
Aside from the typo mentioned above, the code seems correct. What’s the issue, exactly?

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:09 
I think the question is whether it is possible to make tail recursion optimization

-- 23.11.2018, 20:11 --

I believe it is possible, but you may need a structure for quotient numbers

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:18 
Какая оптимизация, ТС "просто не может скомпилировать".

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:40 
Something like that, I think.
Код:
int catalan(int num,int den,int n)
{
     if (n==0)
         return num/den;
     return catalan(2*(2*n-1)*num,den*(n+1),n-1);
}

Should be called initially as "catalan(1,1,n)".
Perhaps, num and den can be divided by their GCD at each iteration, I guess.

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:48 
Да, это в точности то, что уже предлагали по ссылке выше.

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:49 
Оппаньки. :oops:

 
 
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:50 
Вот и мне интересно, чего товарищ ищет ))

 
 
 [ Сообщений: 9 ] 


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group