
This is a beta mode of homepage of Tony
and it will update soon
Bachelor of Optical imformation Science and Technology, Department of Physics
Pursuing PH.D of Applied Mathematic
Sports:Basketball Table Tenis Tenis Swimming
Computer Games:Warcraft III, Counter Strike, Pro Evolution Soccer
I also like go outside and have picnic, as well as go to pub and dancing
| Courses taking this semester | AMS504 | AMS505 | AMS507 | AMS526 | AMS595 | ELS596 |
| Credits | 3 | 3 | 3 | 3 | 1 | 3 |
HOMEWORK 2
Code
#include "stdio.h"
#include "math.h"
int main()
{
long int j = 0, k = 0;
int i = 0, m = 0, a[8] = {0};
long double x = 0, g = 0, t = 0;
while(1)
{
printf( "Welcome to PI calculator\n" );
printf( "\n" );
printf( "0.exit\n1.Series method\n2.Square area approximation method" );
printf( "\n" );
printf( "Please choose the algorithm you want:" );
scanf( "%d",&i );
printf( "\n" );
switch(i)
{
case 0:return 0;
case 1:
{
printf( "Please input the number of significant digits you want,from 1 to 8\n" );//method 1
scanf( "%d",&m );
x = 0;
g = 1;
for (j = 1; j < 999999; j++)//pi/4 = 1 - 1/3 + 1/5 - 1/7......
{
t = 1 / (2 * g - 1);
if ( j % 2 == 0)
t = -t; //add minus sign to the even terms
x = x + t;
g = g + 1;
}
x = x * 4;
x = x * 100000000;//take 8 digits after point
k = (int)x;
for (j = 8; j >=0; j --)// store the 8 digit in array a[]
{
a[j] = k % 10;
k = k / 10;
}
printf("The result is:%d.", a[0]);
for (j = 1; j <= m; j++)
printf("%d", a[j]);
printf("\n");
break;
}
case 2:
{
printf( "Please input the significant digit you want\n" );//method 2
scanf( "%d",&m );
x = 4 * asin(1) / 1000000; //Find the angle which is 1 / 10000000 of 360 degree
g = sin(x / 2) * 1000000; //find its sine, suppose the radius of the circle is 1
g = g * 10000000; //the following is the same as method 1
k = (int)g;
for (j = 7; j >=0; j --)
{
a[j] = k % 10;
k = k / 10;
}
printf("The result is:%d.", a[0]);
for (j = 1; j <= m; j++)
printf("%d", a[j]);
printf("\n");
break;
}//end switch
}//end while
}
return 0;
}
Introduction
Method 1, use the series pi / 4 = 1 ¨C 1/ 3 + 1/ 5 ¨C 1/ 7¡¡.
Method 2, divide the circumference of a polygon by its diameter , the result will be a approximation of PI.
Use Array to make the choosing of significant digit available.
Put the eight significant digit to the array, and output them as intended.