




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: COMPUTATIONAL SCIENCE I; Subject: Comp. & Applied Mathematics; University: Rice University; Term: Fall 2007;
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





In this homework we were asked to write different C programs to evaluate SN , where
n=
n
4
. (1.1)
There are two usual methods to accomplish this job. One is to directly compute the sum using
a ’for’ loop, the other one is to find the formula for SN. The limit of ’int’ type in C language
prevents us to calculate out the right answer when n is large enough. From this experience,
we obtained some techniques when handling large numeber using ’int’ type.
In the part 2, we were required to write C program to implement the algorithm in Home-
work 1 for computing π.
The formula for SN as a 5
th order polynomial of N can be assumed as,
SN = a 0 + a 1 N + a 2 N
2
3
4
5 (2.1)
where the coefficients (a 0 , · · · , a 5 ) can be derived from the following linear equations,
1 1 1 1 1 1
1 2 2
2 2
3 2
4 2
5
2 3
3 3
4 3
5
2 4
3 4
4 4
5
2 5
3 5
4 5
5
2 6
3 6
4 6
5
a 0
a 1
a 2
a 3
a 4
a 5
Then we can obtain this formula,
5
4
3 −
The Rabinowitz and Wagons spigot algorithm, which is based on the following expansion,
was used to compute the digits of π,
π =
∞ ∑
i=
(i!)
2 2
i+
(2i + 1)!
3 Computation Results
We have done the calculations of SN for N = { 3 , 4 , 50 , 51 , 100 , 200 }. The C version program
summation.c calculates the values using both integer precision and double precision. In the
the Matlab version program summation.m there is no distinction between double and integer
precision [1].
The results are given in the following table:
SN sum in C
(int)
formula in
C (int)
sum in C (dou-
ble)
formula in C
(double)
sum in Mat-
lab
formula in
Matlab
S 3 98 98 98.00 98.00 98 98
Table 1: the computed results for SN with different methods. In this table ’sum’ stand for
the direct computing method and ’formula’ for calculations with (2.3) [1]
The following table lists the value of each required digit,
digit 119 1334 19998 999990
value 6 0 5 1
Table 2: the computed digits of π [1]
The following tabel lists the time for computing the given digits using Matlab and C
language,
Digit of π Time needed in MATLAB Time needed in C
100 0.05s 0.00s
1000 2.99s 0.56s
10000 189.20s 26.66s
1000000 >> 72h 72h27m41.00s
Table 3: the time consuming in Matlab and C language [1]
for (n=1;n<=N; ++n)
sum_1 += nnn*n;
sum_2 = -1.0/30N+0.2NNNNN+0.5NNNN+1.0/3NN*N;
printf("Values calculated in integer precision\n");
printf("sum_1 = %d\nsum_2 = %d\n", sum_1, sum_2);
}
/* This function does the same as do_int but with double precision */
void do_double(int N)
{
double sum_1;
double sum_2;
int n;
sum_1=0.0;
for (n=1;n<=N; ++n)
sum_1 += nnn*n;
sum_2 = -1.0/30N+0.2NNNNN+0.5NNNN+1.0/3NN*N;
printf("Values calculated in double precision\n");
printf("sum_1 = %f\nsum_2 = %f\n", sum_1, sum_2);
}
main(int argc, char *argv[]) {
int N;
/* Get value from command line */
N= (int) atoi(argv[1]);
/* Do calculations and print output */
printf("N=%d\n",N);
printf("-------\n");
do_int(N);
printf("-------\n");
do_double(N);
}
summation.m
function summation(N)
sum_1 = 0;
for i=[1:N]
sum_1=sum_1+i^4;
end
sum_2 = N^5/5+N^4/2+N^3/3-N/30;
sum_
sum_
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/* Declare functions */
void init(int*, int);
void print_array(int*, int);
void fix(int *,int);
int proc_digit(int*, int);
void find_prime(int*, int, int);
/* Intialize a vector to 2 in all entries */
void init(int* A, int N)
{
int i;
for(i=0; i<N; i=i+1)
A[i]=2;
}
/* Optional routine for printing an entire array */
void print_array(int *B,int N)
{
int i;
for(i=0;i<N;++i)
printf("%d\n", B[i]);
printf("----------\n");
}
/* Fix all elements that are on value 10 */
void fix(int *B, int n)
{
int i;
for(i=n-1;i>=0;--i) {
if(B[i]==10) {
B[i]=0;
B[i-1]+=1;
}
break;
}
}
}
main(int argc, char *argv[]) {
int *A; int *D;
int j,n,N,p;
clock_t c1, c2;
if (argc != 3) {
printf("Wrong number of arguments!\n");
printf("Please use: ./summation N flag\n");
exit(1);
}
n = (int) atoi(argv[1]);
p = (int) atoi(argv[2]);
N = (int) floor(10*(n+0.0)/(3.0));
A = calloc(N, sizeof(int));
D = calloc(n, sizeof(int));
/* Initialize matrix A */
init(A,N);
c1=clock();
/* Calculate the digits one by one */
for(j=0;j<n;++j)
D[j] = proc_digit(A,N);
/* Correct the erroneous digits with value 10 */
fix(D,n);
/* Optional output */
/print_array(D,n);/
/* Time and print output */
printf("------------\n");
printf("The digit value is: %d\n", D[j-1]);
c2=clock();
printf("It took: %f seconds\n", (double) (c2-c1)/CLOCKS_PER_SEC);
printf("------------\n");
/* Find the prime number with p digits */
if(p>0) {
c1=clock();
find_prime(D, n, p);
c2=clock();
printf("It took: %f seconds\n", (double)(c2-c1)/CLOCKS_PER_SEC);
}
printf("------------\n");
free(D); free(A);
}
References
[1] Eelco Nederkoor. Caam 420: Computational science 1 Problem Set 2.