Welcome, guest | Sign In | My Account | Store | Cart

Pi is a constant, an irrational number and its notation represent the sixteenth letter of the Greek alphabet, though its value is equal to the ratio of a circle’s circumference to its diameter.

However,a Muslim scientist Abu-Abdullah Muhammad Ibn Musa al-Khwarizmi the founder of algebra ( derived from the Arabic word El-Jabr ), arithmetic, and trigonometry, an astronomer, and a geographer C850 studied and proved Pi’s irrationality.(algorithm is derived from his name Khwarizmi ).

I used the circle equation x²+y² = 1 of radius 1 centred at (0,0) and the circle area’s formula to approximate Pi value. y²= 1-x² and y = square-root(1-x²) is the upper semicircle which will extend from x = -1 to x = 1 and the rectangle method is used to approximate this area of an interval [-1,1].

The area of a circle is A = π r², thus the semicircle of radius 1 will have an area A = ½ π(1²).

Using my Pi_Approximation program allows you to represent Pi to infinity of digits, and this will depend of the project type and the precision needed, though space programs require to approximate pi at its maximum precision.

And my Pi_Approximation program allows you to make a better Pi’s approximation than the scientific Calculators

Python, 88 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#On the name of ALLAH and may the blessing and peace of Allah 
#be upon the Messenger of Allah Mohamed Salla Allahu Aliahi Wassalam.
#Author : Fouad Teniou
#Date : 23/02/10
#version :2.6

"""
Pi_approximation uses the subinterval_length function and return
its value and yield all the points values while using the subinterval_point
function and Pi function compute Pi approximation with 16 decimal places
the greater the value of the number the more precise is Pi value 
"""

class Pi_Approximation(object):
    """
    Class that represent Pi approximation
    """
    def __init__(self, number):
        """
        Pi_Approximation constructor takes the number constant
        """
        self.number = number
        
    def subinterval_length(self):
        """
        Compute subinterval_length 
        """
        sub_length = 2/float(self.number)
        return sub_length
    
    def subinterval_point(self):
        """
        Compute the value of each point 
        """

        #attempt to yield all the Xk points values using the subinterval_point        
        try:
            for item in range(1,self.number + 1):
                sub_point = -1 + ((item - 1/2.0)* Pi_Approximation.subinterval_length(self))
                yield sub_point
        
        #Raise TypeError if input is not numerical
        except TypeError:
            print "\n<The entered value is not a number"
    
    def Pi(self):
        """
        Computing Pi value.
        """

        #attempt to Approximate Pi for a given value        
        try:
 
            my_sum = 0    #Set my_sum to 0
            
            # using subinterval_point function to compute Pi approximation.
            # the greater the value of the number the more accurate result
            for self.point in Pi_Approximation.subinterval_point(self):
            
                self.pi_X = pow((1-self.point**2),0.5)
                my_sum += self.pi_X
                pi = (my_sum * Pi_Approximation.subinterval_length(self))*2
                yield repr(pi)
                
        #Raise TypeError if input is not numerical
        except TypeError:
            print  "\n<The entered value is not a number" 

if __name__ == '__main__':
    for arg in xrange(600000,2700000,300000):
    
        pi = Pi_Approximation(arg)
        for i in pi.Pi():
            pass
        print i

#######################################################################
# FT python "C:\Users\Pi1.py"
#
# 3.1415926556860203
# 3.1415926547308004
# 3.1415926543309367
# 3.1415926541200871
# 3.1415926539930723
# 3.141592653909842
# 3.14159265385199

#######################################################################

6 comments

Gabriel Genellina 14 years, 1 month ago  # | flag

For an infinite precision generator for π, see Demo/scripts/pi.py in the Python source distribution.

Stephen Chappell 14 years, 1 month ago  # | flag

This is a rather simple solution for π as well:

def pi():
    a, b, c, d, e, f = 1, 0, 1, 1, 3, 3
    while True:
        if a * 4 + b - c < c * e:
            yield e
            a, b, c, d, e, f = a * 10, (b - c * e) * 10, c, d, ((a * 3 + b) * 10) // c - e * 10, f
        else:
            a, b, c, d, e, f = a * d, (a * 2 + b) * f, c * f, d + 1, ((d * 7 + 2) * a + b * f) // (c * f), f + 2

digit = pi()
print(next(digit), next(digit), sep='.', end='')
while True:
    print(next(digit), end='')
Fouad Teniou (author) 14 years, 1 month ago  # | flag

Thank you Stephen Chappell,

Not as simple as the decimal_function function from my Binary_Decimal Recursion program. Though I used the re module and the ‘.’ to make it as simple.

Gabriel Genellina 14 years, 1 month ago  # | flag

Note that both the demo script I posted earlier and the above code by S. Chappell only use integer arithmetic and achieve infinite precision (limited by available memory only). Your version is limited by the floating point precision, about 16 digits.

Fouad Teniou (author) 14 years, 1 month ago  # | flag

Thank you Gabriel Genellina,

There is no floating point limitation if you use the decimal module getcontext().prec function, or other pythons hidden functions such as repr, thus, an infinity precision, and space programs

Fouad Teniou (author) 12 years, 8 months ago  # | flag

A switch to C# and a new link

http://archive.msdn.microsoft.com/fouadteniou