C language (C). The result is not what I would like. Implemented without recurrent formulas and did something wrong. Bernoulli numbers seem to be correct.
#include <stdio.h> #include <conio.h> #include <math.h> long int Factorial(long int x) { //функция вычисления факториала if (x == 0 || x == 1) return 1; //по определению факториал нуля и единицы равен единице else return x*Factorial(x-1); //функция вызывается внутри себя (рекурсия) } float Bink (long int n, long int k) { //Биномиальный коэффициент Ньютона return 1.0*Factorial(n)/Factorial(k)/Factorial(nk); } float Bernoulli(long int n) { //Число Бернулли if (n<=0) return 1; else { float s = 0; long k; for(k = 1;k<=n;k++) s+=Bink(n+1,k+1)*Bernoulli(nk); return -1.0/(n+1)*s; } } float Cotangent(int x) { int i = 1; float p = 1.0, s = 1.0, eps = 0.00001; for (;fabs(p)>eps;i++) { p = (pow(2,2*i)*Bernoulli(i)*pow(x,2*i-1))/Factorial(2*i); s+=p; #ifdef DEBUG printf("\ni = %i, p = %f, s = %f",i,p,s); //отладочные сообщения #endif } return 1/(float)x - s; } int main() { printf("\tЧИСЛА БЕРНУЛЛИ:\n"); for (i = 0; i<=10; i++) printf("B[%d] = %f\n", i, Bernoulli(i)); printf("\n\n\tРАЗЛОЖЕНИЕ ФУНКЦИИ\n\n"); printf("\n\n%f", Cotangent(1)); return 0; } Attaching debug results
As you can see, the result (0.888889) does not at all coincide with the real value of the function (cot (1) = 0.642092). What is the matter?

