Introduction to PI CALCULATION PROGRAM Hongyan Chen (ID: 106508158) My program shows three methods to calculate the pi value. Users are able to select one of them and set the corresponding significant digits. I already put some codes for input checking. I. The first algorithm is based on probability theory. For two independent uniform random variables x, y in [0,1], pi/4 = p{x^2+y^2<1}. This method is quite time consuming, If the user chooses n significant digits, 4*10^n trials will be necessary. Also, the result is only an approximation to pi since different runs with the same significant digits may produce different results .Thus I limit the accuracy up to 9 digits. II. The second algorithm uses the formula: pi = 3* 2^n * sqrt(2-sqrt(2+sqrt(2+sqrt(2+...sqrt(2+1))))), which is derived geometrically. It can be also identified as a series. Compared with the probability method, it converges to pi much faster. For both methods, I only use a double variable to store the pi value. Therefore, the maximum significant digits is limited (16~17). I believe this formula can be also used for calculation with numerous significant digits. However, squar root computation/FFT is involved here, I can not figure out how to do it. III. The last method is a much more accurate way. Another series is applied: pi/2 = 1 + 1/3 + 1/3 * 2/5 + 1/3 * 2/5 * 3/7 +... In order to get numerous significant digits, I use arrays to store numbers, one digit per element. The principle is that I store every varible in a form already with numerous digits. e.g. 1 = 1.00000......................................., 1/3 = 0.33333..................................... The predefined array's size determines how many digits we have. Afterwards, simple algebraic operations can be perfromed (each digit position one time, explaination can be found in the source file). Obviously, My program could be improved since each element in array can store more than 1 digit, just be aware of the long/int type variable's precision. I did not do it because the current codes are easier to be understood. Nevertheless, there is one main drawbacks in my codes. The array's size is previously defined, which means I have already truncated every value before operation. Such error can be accumulated from round to round. Moreover, it affect the flexibility and efficiency of my program. If I want to get significant digits more than the size of array, I have to change the codes; If I only want a reasonable accuracy, a lot of computation is not necessary (But the input significant number is still useful to terminate iteration timely, pls see the explaination in the source file). According to the above discussion, I set my arraysize to 6000, and limit the accuracy up to 5000. IV. I must say I met a trouble when debugging the program. When I set 7 significant digits for the second method, it always display 6 digits even I used setprecision(7).