fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef unsigned int Uint;
  5. #define N 1000
  6. Uint DP=1;
  7. //RADIX == 10^LOG
  8. #define LOG 1
  9. #define RADIX 10
  10. typedef Uint Liczba[N+1];
  11.  
  12. void pisz( Liczba a, int ctrl){ Uint i;
  13. for( i=1; i<DP && !a[i]; i++ ) printf("%*s", LOG,"");
  14. printf("%*u", LOG, a[i]);
  15. for( i++; i<=DP; i++ ) printf("%0*u", LOG, a[i]);
  16. putchar('.');
  17. for( ; i<=N; i++ ) printf("%0*u", LOG, a[i]);
  18. if( ctrl < 0 ) puts("");}
  19.  
  20. void plus( Liczba a, Liczba b){ // a[]:=a[]+b[]
  21. for( Uint c=0, i=N; i>0; i-- ){
  22. c += a[i]+b[i];
  23. a[i] = c % RADIX;
  24. c /= RADIX;}}
  25.  
  26. void minus( Liczba a, Liczba b ) { // a[]:=a[]-b[]
  27. for( Uint c=0, i=N; i>0; i-- ){
  28. c += b[i];
  29. if( a[i]<c ){
  30. a[i] = RADIX + a[i] - c; c=1; }
  31. else {
  32. a[i] -= c; c=0; }}}
  33.  
  34. void razyU( Liczba a, Uint b ) { // a[]:=a[] * b
  35. for( Uint c=0,i=N; i>0; i-- ){
  36. c += a[i]*b;
  37. a[i]= c % RADIX;
  38. c /= RADIX; }}
  39.  
  40. int podzielU( Liczba w, Liczba a, Uint b ) { // w[]:=a[] / b
  41. Uint z=0; // return w[] != 0
  42. for( Uint c=0, i=1; i<=N; i++ ) {
  43. c = c*RADIX+a[i];
  44. w[i]= c/b % RADIX; if( w[i] ) z=1;
  45. c = c - w[i]*b;}
  46. return z;}
  47.  
  48. void wstawU( Liczba a, Uint b ) { // a[]:= b
  49. Uint i;
  50. for( i=0; i<=N; i++ ) a[i]=0;
  51. i=DP;
  52. while( b ) {
  53. a[i--] = b%RADIX; b /= RADIX; }}
  54.  
  55. void atg( Liczba a, Uint x ) { // a[]:= atan( 1/x )
  56. Liczba px;
  57. wstawU( px, 1 ); podzielU( px, px, x );
  58. wstawU( a, 0 ); plus ( a, px );
  59. for( Uint n=1; ; n++){
  60. Liczba b;
  61. podzielU( px, px, x*x );
  62. if( ! podzielU( b, px, 2*n + 1 ) )
  63. break;
  64. if( n & 1 ) minus( a, b );
  65. else plus( a, b );}}
  66.  
  67. int main(void) {
  68. Liczba a1, a2;
  69. atg( a1, 5 ); razyU( a1, 4 );
  70. atg( a2, 239);
  71. minus( a1, a2 );
  72. razyU( a1, 4 );
  73. pisz( a1, -1 );
  74. puts( "3,141592653589793238462643383279502884197169399375"
  75. "10582097494459230781640628620899862803482534211706"
  76. "79821480865132823066470938446095505822317253594081"
  77. "28481117450284102701938521105559644622948954930381");
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.09s 1720KB
stdin
Standard input is empty
stdout
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420124
3,141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381