Lab 1 Solution C source from internet

来源:互联网 发布:遗传算法与svm python 编辑:程序博客网 时间:2024/05/01 22:46
copy from :
http://www.ntnu.edu.tw/acm/ProblemSetArchive/B_US_EastCen/1991/table.c
/* ACMPROB4.C
   Solution to the ACM East Central Region Programming Contest
   Problem #4 - Summation of a series
*/

#include
<stdio.h>

main()
{
    
long k;        /* dummy summation variable */
    
long n;        /* number of terms to sum */
    
double kd;        /* double version of k */
    
double x;        /* the variable */
    
double sum;        /* used for computation summations */
    
double px, p1;    /* psi(x) and psi(1) */
    
double dpx;        /* value of the first accelerated series at x */
    
double dp2;        /* value of the first accelerated series at 2 */
    
double ddpx;    /* value of the second accelerates series at x */
    
long i;        /* counter for elements of table */

    p1 
= 1.0;
    dp2 
= 0.25;
    n 
= 2000;

/*  evaluation of the error term for the summation
    e = 1.0 / ( ( r+1.0)*pow((double)n+1.0,(couble)r-1.0));,
    shows that n=120000 is large enough.  If the first
    improved sequence is used.  With approximately 16 digits
    of precision in a double precision variable, adding this
    many positive decreasing terms will not effect the
    desired final precision, but it is close.

    For even more speed and more precision without roundoff,
    a second application of the acceleration method produces
    a similar series which requires only 2000 term.
*/


/* loop for the 21 table entries */
    
for (i=0; i<21; i++ )
    
{
    x 
= i * 0.1;

    
/* sum second accelerated series */
    sum 
= 0.0;
    
for( k=1; k<n; k++ )
    
{
        kd 
= k;
        sum 
+= 1.0/(kd*(kd+1)*(kd+2)*(kd+x) );
    }

    ddpx 
= (2.0-x)*sum;

    
/* get the value of the first accelerated series */

    dpx 
= (1.0-x)*(dp2+ddpx);

    
/* get value of original series */

    px 
= p1+dpx;

    printf(
"%4.2f %16.12f ", x, px );
    }

}


/*
    Correct Answers to Series Summation Problem

    Here is the actual solution correct to a digit in the last place.
    All student solutions should agree with this up to the last two
    places.  That is, a valid student solution may differ from this
    solution by plus or minus 1 is the third digit from the right.

    0.00   1.644934066848
    0.10   1.534607244904
    0.20   1.440878841547
    0.30   1.360082586782
    0.40   1.289577800791
    0.50   1.227411277760
    0.60   1.172105196125
    0.70   1.122519342536
    0.80   1.077758872744
    0.90   1.037110917851
    1.00   1.000000000000
    1.10   0.965956030529
    1.20   0.934590918036
    1.30   0.905581188666
    1.40   0.878654881869
    1.50   0.853581537031
    1.60   0.830164448547
    1.70   0.808234608172
    1.80   0.787645918751
    1.90   0.768271376600
    2.00   0.750000000000
*/

 
原创粉丝点击