Loops are often used in programs that compute numerical results by starting with an approximate answer and iteratively improving it.

For example, one way of computing square roots is Newton’s method. Suppose that you want to know the square root of a. If you start with almost any estimate, x, you can computer a better estimate with the following formula:

For example, if a is 4 and x is 3:

Which is closer to the correct answer. If we repeat the process with the new estimate, it gets even closer:

After a few more updates, the estimate is almost the exact:

When y == x, we can stop. Here is a loop that starts with an initial estimate, x, and improves it until it stops changing:

For most values of a this works fine, but in general it is dangerous to test float equality. Floating-point values are only approximately right: most rational numbers, like 1/3 and irrational numbers, like , can’t be represented exactly with a float.

Rather than checking whether x and y are exactly equal, it is safer to use math.fabs to compute the absolute value, or magnitude, of difference between them:

If math.fabs(y-x) < something_small: break

Where something_small has a value like 0.000001 that determines how close is close enough.

Wrap this loop in a function called square_root that takes a as parameter, choose a reasonable value of x, and returns an estimate of the square root of a.

from Thinking in Python

Square roots的更多相关文章

  1. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  2. 欧拉工程第64题:Odd period square roots

    题目链接 找循环位数是奇数的数有多少个 这个自己很难写出来,完全不能暴力 维基百科链接 维基百科上面说的很好,上面的算法实现就好了. 就是上面的 Java程序: package project61; ...

  3. UVa 1426 Discrete Square Roots (扩展欧几里德)

    题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...

  4. Discrete Square Roots UVALive - 4270(拓展欧几里得)

    a≡b(mod n)的含义是“a和b除以n的余数相同”,其充要条件是“a-b是n的整数倍”: 求所有满足条件r^2=x(mod m)的r 题目已经给定了一个初始的r,x,m #include < ...

  5. UVALive 4270 Discrete Square Roots

    题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...

  6. UVALive - 4270 Discrete Square Roots (扩展欧几里得)

    给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...

  7. UVA1426 Discrete Square Roots

    思路:\(exgcd\) 提交:\(2\)次 错因:输出格式错误OTZ 题解: 求:\(r^2 ≡ x \mod N , 0 \leq r < N\),并且题目会给出 \(x,N\) 和一个合法 ...

  8. [MIT6.006] 12. Square Roots, Newton's Method 平方根,牛顿法

    首先让我们回顾下上节课讲的,用牛顿法计算√2的内容: 简单来说,牛顿法从x0=1不断向后计算逼近√2的值,而刚开始计算的精度是1,随着牛顿法的逼近(共log2d个循环),就能使得√2逼近值的精度达到d ...

  9. Project Euler 80:Square root digital expansion 平方根数字展开

    Square root digital expansion It is well known that if the square root of a natural number is not an ...

随机推荐

  1. hdu 1722 Cake 数学yy

    题链:http://acm.hdu.edu.cn/showproblem.php? pid=1722 Cake Time Limit: 1000/1000 MS (Java/Others)    Me ...

  2. ADO.NET (二)—— ADO和ADO .NET对照

     ADO.NET (二)-- ADO和ADO .NET对照       我们知道ADO.NET的两大核心组件各自是Data Provider和DataSet.假设说 DataSet是ADO.NET的心 ...

  3. 带输出參数的存储过程的定义,以及在aso.net中调用

    ALTER proc [dbo].[mp_w_RechargePortalPayPal_All] ( @PayPalOrderNo nvarchar(50), --订单号 @nAccountIDFro ...

  4. NOIP2017提高组模拟赛 7(总结)

    NOIP2017提高组模拟赛 7(总结) 第一题 斯诺克 考虑这样一个斯诺克球台,它只有四个袋口,分别在四个角上(如下图所示).我们把所有桌子边界上的整数点作为击球点(除了4个袋口),在每个击球点我们 ...

  5. [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...

  6. Ubuntu下gcc安装

    在Ubuntu下安装GCC和其他一些Linux系统有点不一样. 方法一: 该方法超简单:sudo apt-get  build-depgcc 就上面这条命令就可以搞定 方法二:sudo apt-get ...

  7. Swift学习笔记(8):闭包

    目录: 基本语法 尾随闭包 值捕获 自动闭包 闭包是自包含的函数代码块,闭包采取如下三种形式之一: ・全局函数是一个有名字但不会捕获任何值的闭包 ・嵌套函数是一个有名字并可以捕获其封闭函数域内值的闭包 ...

  8. 继续ubuntu和遇到的easybcd的坑

    找了很多教程,反复斟酌https://www.zhihu.com/question/34611974 个人感觉前方还有大坑无数.unbuntu四个分区系列——65%根目录,25%home目录,交换分区 ...

  9. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  10. 【转载】tom的RUNSTATS测试工具

    -- 创建 runstats 包.其中包括 3 个简单 API 调用: create or replace package runstats_pkg as procedure rs_start; pr ...