※ 引述《yoyodiy (廢文心得文大師)》之銘言:
: python 自學怎麼這麼難
: 有看沒有懂 難度比寫出一個99乘法表還難萬倍
: 如果不借助網路 怎樣自學最快?
: 如果鍵盤輸入任一數 求平方根
: 要寫出這樣的程式大概要學幾天?
: 有八卦嗎?
教你一個2000年前就有的方法
巴比倫法(Babylonian method)
1.給個數字A
2.猜他的平方根b
3.把猜的數字平方後,與A相減 就是| A-b^2 |
4.假如減後絕對值<可容許誤差範圍 本片結束
5.不然就計算新的猜測平方根=[b+(A/b)]/2
6.回步驟3
巴比倫人能計算方根2到 1.414212962962....,誤差少於百萬份之一
挖出來的證據
user_response=input("Enter a number:")
number=float(user_response)
guess=number/2
accuracy=0.01
iteration=0
while abs(number-(guess**2))>accuracy:
print("Iteration",interation,"Guessed square is:",guess)
guess=(guess+(number/guess))/2
iteration=iteration+1
print("Original number is:",number)
print("Square root of the number is:",guess)