Square roots
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的更多相关文章
- UVA 1426 - Discrete Square Roots(数论)
UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...
- 欧拉工程第64题:Odd period square roots
题目链接 找循环位数是奇数的数有多少个 这个自己很难写出来,完全不能暴力 维基百科链接 维基百科上面说的很好,上面的算法实现就好了. 就是上面的 Java程序: package project61; ...
- UVa 1426 Discrete Square Roots (扩展欧几里德)
题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...
- 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 < ...
- UVALive 4270 Discrete Square Roots
题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...
- UVALive - 4270 Discrete Square Roots (扩展欧几里得)
给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...
- UVA1426 Discrete Square Roots
思路:\(exgcd\) 提交:\(2\)次 错因:输出格式错误OTZ 题解: 求:\(r^2 ≡ x \mod N , 0 \leq r < N\),并且题目会给出 \(x,N\) 和一个合法 ...
- [MIT6.006] 12. Square Roots, Newton's Method 平方根,牛顿法
首先让我们回顾下上节课讲的,用牛顿法计算√2的内容: 简单来说,牛顿法从x0=1不断向后计算逼近√2的值,而刚开始计算的精度是1,随着牛顿法的逼近(共log2d个循环),就能使得√2逼近值的精度达到d ...
- 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 ...
随机推荐
- JavaCodeTra 36选7 彩票抽奖
想写个小代码试试自己的运气.然并卵.并不能猜中 import java.util.Random; import java.util.Scanner; /** * */ /** * @author Ha ...
- gdb学习-checkpoint,watch
checkpoint的内容参考: http://blog.chinaunix.net/uid-23629988-id-2943273.html 这一篇主要是checkpoint,在next之前加che ...
- 一起talk C栗子吧(第一百一十二回:C语言实例--线程同步概述)
各位看官们,大家好.上一回中咱们说的是线程间通信的样例,这一回咱们说的样例是:线程同步.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,提到同步.我想大家都不陌生,由于我们在前面章回中介绍 ...
- [cocos2dx笔记013]一个使用CCRenderTexture创建动态纹理显示数字的类
用CCLabelTTF显示的数字不好看.于是就想到用图片来代理.眼下网上的实现都是把每一个数字做一个CCSprite组合的方式. 可是我想.动态生成纹理的方式.没有就仅仅好自己手动写一个. 头文件 # ...
- 使用Java语言实现,自己主动生成10个整数(1~100,求出生成数列中的最大值和最小值,不同意使用Arrays类的sort方法
这是考察主要的java基础,没啥难点,直接上代码,近期在准备面试,所以做一些基础的面试题练练手 public class Demo1 { public static void main(String[ ...
- UI_UIImagePickerController(读取图片)
创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...
- ioctl.h 分析
ioctl.h 分析 我自己画了个解析图...不要嫌弃丑啊.. . 哈哈 type The magic number. Just choose one number (after consulting ...
- C#中的CSP(Communicating sequential processes)
说起Golang(后面统称为Go),就想到他的高并发特性,在深入一些就是 Goroutine.在大家被它优雅的语法和简洁的代码实现的高并发程序所折服时,其实C#/.NET也可以很容易的做到.今天我们来 ...
- HD-ACM算法专攻系列(10)——大明A+B
题目描述: 源码: 需要注意的一点是输出是最简形式,需要去除小数的后导0,而调用stripTrailingZeros()函数后,数会以科学计数法输出,所以需要调用toPlainString(). im ...
- HD-ACM算法专攻系列(5)——N!
题目描述: 源码: #include"iostream" using namespace std; int main() { int n, digit, carry, tmp; i ...