Home  >  Q&A  >  body text

A python novice wants to make a code that can calculate pi, but I don’t know how to write it.

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?

高洛峰高洛峰2686 days ago949

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-12 09:23:09

    >>> from math import pi
    >>> pi
    3.141592653589793

    Computing Pi with Python

    reply
    0
  • Cancelreply