bernum.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* This program computes the Bernoulli numbers.
  2. * See radd.c for rational arithmetic.
  3. */
  4. typedef struct{
  5. double n;
  6. double d;
  7. }fract;
  8. #define PD 44
  9. fract x[PD+1] = {0.0};
  10. fract p[PD+1] = {0.0};
  11. #include <math.h>
  12. #ifdef ANSIPROT
  13. extern double fabs ( double );
  14. extern double log10 ( double );
  15. #else
  16. double fabs(), log10();
  17. #endif
  18. extern double MACHEP;
  19. main()
  20. {
  21. int nx, np, nu;
  22. int i, j, k, n, sign;
  23. fract r, s, t;
  24. for(i=0; i<=PD; i++ )
  25. {
  26. x[i].n = 0.0;
  27. x[i].d = 1.0;
  28. p[i].n = 0.0;
  29. p[i].d = 1.0;
  30. }
  31. p[0].n = 1.0;
  32. p[0].d = 1.0;
  33. p[1].n = 1.0;
  34. p[1].d = 1.0;
  35. np = 1;
  36. x[0].n = 1.0;
  37. x[0].d = 1.0;
  38. for( n=1; n<PD-2; n++ )
  39. {
  40. /* Create line of Pascal's triangle */
  41. /* multiply p = u * p */
  42. for( k=0; k<=np; k++ )
  43. {
  44. radd( &p[np-k+1], &p[np-k], &p[np-k+1] );
  45. }
  46. np += 1;
  47. /* B0 + nC1 B1 + ... + nCn-1 Bn-1 = 0 */
  48. s.n = 0.0;
  49. s.d = 1.0;
  50. for( i=0; i<n; i++ )
  51. {
  52. rmul( &p[i], &x[i], &t );
  53. radd( &s, &t, &s );
  54. }
  55. rdiv( &p[n], &s, &x[n] ); /* x[n] = -s/p[n] */
  56. x[n].n = -x[n].n;
  57. nx += 1;
  58. printf( "%2d %.15e / %.15e\n", n, x[n].n, x[n].d );
  59. }
  60. }