A python novice wants to make a code that can calculate pi, but I don't know how to write it.
高洛峰
高洛峰 2017-06-12 09:21:09
0
1
1010

I wrote a piece of code that can calculate the factorial of a number, as follows:

#-*- coding:gb2312 -*-
#关于无限阶乘
result = 1
i = 1
while i <=50:
    result *= i
    i += 1
    print(result)

If you change while i <=50 to any number, you can calculate any factorial, or if you change it to while True, you can also change it to infinite factorial.

My question:
I currently want to follow this idea and write a piece of code. After execution, the result is pi, and the desired number of digits can be retained.
For example, I can choose to keep 10 decimal places, or keep 20 decimal places. I don’t know how to write it, and I don’t have any ideas.
I wonder if you can give me some tips?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
过去多啦不再A梦
>>> from math import pi
>>> pi
3.141592653589793

Computing Pi with Python

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template