牛顿法是一个迭代算法,原理用一句话就可以概括:如果 a * b = n,那么 n的平方根一定在a与b之间 换句话说 (a + b) /2 ,一定比a更精确 package puzzles.sqrt /** * Created by Bo on 2015/1/1. */ import scala.math.abs object Newton extends App{ def sqrt(num: Double): Double = { def iter(guess: Double)
上课教的内容.做笔记了. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 平方计算器 { public partial class Form1 : Form {
from math import sqrt def mysqrt(num,small): assert num>0 assert small>0 low = 0.0 high = max(num,1) loops=1 while True and loops<=100: a = (high + low) / 2 test = a ** 2 if abs(test-num)<small: