2017-07-31 136 views
2

w3resources的挑戰之一是將pi打印到'n'小數位。這裏是我的代碼:將pi打印到小數位數 - Python

from math import pi 

fraser = str(pi) 

length_of_pi = [] 

number_of_places = raw_input("Enter the number of decimal places you want to 
see: ") 

for number_of_places in fraser: 
    length_of_pi.append(str(number_of_places)) 

print "".join(length_of_pi) 

無論出於何種原因,它會自動打印pi而不考慮任何輸入。任何幫助將是巨大的:)

+0

打印'pi' *舍入*到n個十進制數字,或打印'pi'的前n個十進制數*是挑戰嗎? – glibdud

回答

7

爲什麼不直接使用formatnumber_of_places

''.format(pi) 
>>> format(pi, '.4f') 
'3.1416' 
>>> format(pi, '.14f') 
'3.14159265358979' 

更普遍:

>>> number_of_places = 6 
>>> '{:.{}f}'.format(pi, number_of_places) 
'3.141593' 

在你原來的做法,我想你想挑使用number_of_places作爲循環的控制變量的數字位數,這是相當黑的,但不適用於您的情況,因爲用戶輸入的初始number_of_digits從不使用。它被替換爲來自pi字符串的迭代值。

+0

我認爲這有點擊敗了整個挑戰?有沒有辦法修改對應給定輸入的小數位數? – Fraser

+0

@Fraser答案中有*更一般的*部分:) –

1

使用np.pimath.pi等提出的解決方案也只是工作,以雙精度(〜14位),以獲得更高的精度,你需要使用多精度,例如mpmath包

>>> from mpmath import mp 
>>> mp.dps = 20 # set number of digits 
>>> print(mp.pi) 
3.1415926535897932385 

使用np.pi給出錯誤的結果

>>> format(np.pi, '.20f') 
3.14159265358979311600 

比較真值:

3.14159265358979323846264338327... 
0

您的解決方案似乎遍歷錯誤的事情:

for number_of_places in fraser: 

對於9位,這原來是這樣的:

for "9" in "3.141592653589793": 

哪些循環三次,每一個「9」在字符串中找到。我們可以解決您的代碼:

from math import pi 

fraser = str(pi) 

length_of_pi = [] 

number_of_places = int(raw_input("Enter the number of decimal places you want: ")) 

for places in range(number_of_places + 1): # +1 for decimal point 
    length_of_pi.append(str(fraser[places])) 

print "".join(length_of_pi) 

但是,這仍然限制n要小於len(str(math.pi)),小於15的Python 2.鑑於嚴重n,它打破:

> python test.py 
Enter the number of decimal places you want to see: 100 
Traceback (most recent call last): 
    File "test.py", line 10, in <module> 
    length_of_pi.append(str(fraser[places])) 
IndexError: string index out of range 
> 

做的更好,我們要計算PI自己 - 通過一系列的評價是一種方法:

# Rewrite of Henrik Johansson's ([email protected]) 
# pi.c example from his bignum package for Python 3 
# 
# Terms based on Gauss' refinement of Machin's formula: 
# 
# arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + ... 

from decimal import Decimal, getcontext 

TERMS = [(12, 18), (8, 57), (-5, 239)] # ala Gauss 

def arctan(talj, kvot): 

    """Compute arctangent using a series approximation""" 

    summation = 0 

    talj *= product 

    qfactor = 1 

    while talj: 
     talj //= kvot 
     summation += (talj // qfactor) 
     qfactor += 2 

    return summation 

number_of_places = int(input("Enter the number of decimal places you want: ")) 
getcontext().prec = number_of_places 
product = 10 ** number_of_places 

result = 0 

for multiplier, denominator in TERMS: 
    denominator = Decimal(denominator) 
    result += arctan(- denominator * multiplier, - (denominator ** 2)) 

result *= 4 # pi == atan(1) * 4 
string = str(result) 

# 3.14159265358979E+15 => 3.14159265358979 
print(string[0:string.index("E")]) 

現在,我們可以採取的一個較大的值:

> python3 test2.py 
Enter the number of decimal places you want: 100 
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067 
>