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 


23/11/18
1
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 


20/03/14
12041
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 
Заслуженный участник


27/04/09
28128
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 


06/09/17
112
Москва
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 


20/03/14
12041
Какая оптимизация, ТС "просто не может скомпилировать".

 Профиль  
                  
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:40 


14/01/11
2916
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 


20/03/14
12041
Да, это в точности то, что уже предлагали по ссылке выше.

 Профиль  
                  
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:49 


14/01/11
2916
Оппаньки. :oops:

 Профиль  
                  
 
 Re: It is possible to make a catalan number in C with tail
Сообщение23.11.2018, 20:50 


20/03/14
12041
Вот и мне интересно, чего товарищ ищет ))

 Профиль  
                  
Показать сообщения за:  Поле сортировки  
Начать новую тему Ответить на тему  [ Сообщений: 9 ] 

Модераторы: Karan, Toucan, PAV, maxal, Супермодераторы



Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group