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

时间: 2024-09-30 09:44:48

Square roots的相关文章

UVA 1426 - Discrete Square Roots(数论)

UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N, R,要求r2≡x (mod n) (1 <= r < n)的所有解,R为一个已知解 思路: r2≡x (mod n)=>r2+k1n=x 已知一个r!,带入两式相减得 r2?r12=kn => (r+r1)(r?r1)=kn 枚举A,B,使得 A * B = n (r + r1)为A倍数 (r - r1)为B倍数 这样就可以推出 Aka?r1=Bkb+r1=r => Aka=Bk

UVA - 1426 Discrete Square Roots (模方程)

Description A square root of a number x is a number r such that r2 = x. A discrete square root of a non-negative integer x is a non-negative integer r such that r2 x mod N , 0r < N , where N is a specific positive integer and mod is the modulo operat

uva 1426 - Discrete Square Roots(拓展欧几里得)

题目链接:uva 1426 - Discrete Square Roots 题目大意:给出X,N,R,求出所有满足的r,使得r2≡x%N,并且R是一个其中的解. 解题思路: R2?r2=k?N (R?r)(R+r)=k?N => aA=(R+r),bB=(R?r),A,B为N的因子 所以枚举A,B,就有r=R?aA=bB?R aA+bB=2?R 拓展欧几里得求解,将所有满足的解放入set中自动去重. #include <cstdio> #include <cstring> #

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

题目链接 找循环位数是奇数的数有多少个 这个自己很难写出来,完全不能暴力 维基百科链接 维基百科上面说的很好,上面的算法实现就好了. 就是上面的 Java程序: package project61; public class P64{ void run(){ int count = 0; int m = 0; int d = 1; int a0 = 0; int a = 0; int period = 0; for(int S = 2;S<10000;S++){ period = 0; m =

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

给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r')(r-r')=kn$ 将$n$分解成$a*b$,则有$\left\{\begin{matrix}r+r'=xa\\ r-r'=yb\end{matrix}\right.$ 两式相加得$2r=xa+yb$,这是一个二元线性不定方程,可用扩欧求出x的通解. 假设已经求出了$x$的通解$x=x_{0}+k

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 integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all. The square root of two i

使用.net Stopwatch class 来分析你的代码

当我们在调试,优化我们的代码的时候,想知道某段代码的真正的执行时间,或者我们怀疑某段代码,或是某几段代码执行比较慢, 需要得到具体的某段代码的具体执行时间的时候.有一个很好用的类Stopwatch. Stopwatch 类在 System.Diagnostics命名空间下.可以用来做分析.net代码块的基本工具. 例如: System.Diagnostics.Stopwatch timerObj = new System.Diagnostics.Stopwatch(); timerObj.Sta

8.2/baltic神(水)题

bzoj1334: Description N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退出后,其它党的席位仍大于总数的一半,则这个政党被称为是多余的,这是不允许的. Input 第一行给出有多少个政党.其值小于等于300 下面给出每个政党的席位数.总席位数小于等于 100000 Output 你的组阁方案中最多能占多少个席位. 背包dp.一开始想着贪心贪心...后

[转载] CMake Official Tutorial——教程还是官方的好

CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Point (Step1) The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is requ