采用“牛顿法”求一个数的平方根

object sqrt {

  def main(args:Array[String])={
    println( sqrt(args(0).toDouble))
  }
  def sqrt(x:Double)= sqrtIter(1.0,x)
  def sqrtIter(guess:Double,x:Double):Double=
    if(isGoodEnough(guess,x)) guess
    else sqrtIter(improve(guess,x),x)

  def isGoodEnough(guess:Double, x:Double)=
    ((guess * guess -x) < 0.0001*x) && ((guess * guess -x ) > -0.0001*x)

  def improve (guess:Double, x:Double)=
    (guess+ x/guess)/2
}

我们还可以把几个辅助函数直接定义在sqrt函数里面:

object sqrt {

  def main(args:Array[String])={    println( sqrt(args(0).toDouble))  }

  def sqrt(x:Double)={    def isGoodEnough(guess:Double, x:Double)=      ((guess * guess -x) < 0.0001*x) && ((guess * guess -x ) > -0.0001*x)

    def improve (guess:Double, x:Double)=      (guess+ x/guess)/2

    def sqrtIter(guess:Double):Double=      if(isGoodEnough(guess,x)) guess      else sqrtIter(improve(guess,x))

    sqrtIter(1.0)  }}

The End......

Thinking in scala (3)----求平方根的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  2. ytu 1041: 迭代法求平方根(水题)

    1041: 迭代法求平方根 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 227  Solved: 146[Submit][Status][Web Bo ...

  3. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  4. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  5. note 5 二分法求平方根,素数,回文数

    +二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...

  6. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  7. JustOj 1036: 习题6.11 迭代法求平方根

    题目描述 用迭代法求 .求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001. 输出保留3位小数 输入 X 输出 X的平方根 样例输 ...

  8. 二分法求平方根(Python实现)

    使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...

  9. 求平方根C++

    求平方根,正根.曾经都不会.昨天看数学,看到了,写了出来.自己又小优化了一下,非常不错. // squareRoot.cpp -- 2011-08-29-01.04 #include "st ...

随机推荐

  1. 转:Web安全与Rational AppScan入门

    Web 应用的基础概念 在讨论 Web 应用安全之前,先简单介绍一下 Web 应用基础概念,这样便于理解为什么 Web 应用是脆弱的,容易受到攻击. 1. 什么是 Web 应用 Web 应用是由动态脚 ...

  2. panel 绑定鼠标滚轮事件

    void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...

  3. Windows恢复Grub引导,用grub安装ubuntu

    http://www.linuxidc.com/wap.aspx?nid=18027&p=&cp=&cid=http://m.blog.chinaunix.net/uid-22 ...

  4. HBase的Shell命令

    1.HBase提供了一个shell的终端给用户交互 2.HBase Shell的DDL操作 (1)先进入HBase的 Shell命令行,即HBASE_HOME/bin/hbase shell …… & ...

  5. Hibernate中用纯SQL查询,并通过hibernate分页返回List<对象>

    @SuppressWarnings("unchecked") public List<Article> getPageQueryList(final int pageN ...

  6. NSDate,NSCalendar

    NSLog(@"%@",[NSDate date]); 打印结果: 2015-06-30 08:42:14 +0000 把它转换成2015年6月30日 8时42分14秒 怎么转? ...

  7. ARM汇编指令集

    一.跳转指令.跳转指令用于实现程序流程的跳转,在ARM程序中有以下两种方法可以实现程序流程的跳转. Ⅰ.使用专门的跳转指令.Ⅱ.直接向程序计数器PC写入跳转地址值. 通过向程序计数器PC写入跳转地址值 ...

  8. FatMouse and Cheese 动态化搜索

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  10. Redis缓存服务搭建及实现数据读写--转载

    来自 http://www.cnblogs.com/lc-chenlong/p/3218157.html 1.  下载安装Redis 下载地址:https://github.com/MSOpenTec ...