-
Notifications
You must be signed in to change notification settings - Fork 0
/
longSqrt.py
40 lines (34 loc) · 860 Bytes
/
longSqrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#author : https://github.com/Brouss3
#language : python 2.x
#date start : 2018/01/30
#date end : 2018/02/03
#version : 0.0
#licence : GPL
from math import sqrt as msqrt,log as mlog
#original seed square root function
def sqNear0(lf):
assert(lf>=0)
d=int(mlog(lf,10))
r0=10**((d)//2)
fl=lf*10**(2*10)//r0**2
fl=msqrt(fl)
r1=r0*int(fl*10**10)//10**20
return(r1)
#after a while I thought I made the seed overcomplicated
def sqNear1(lf):
le=int(mlog(lf,10)//2)*2
if le <100:
return(int(msqrt(lf)))
a=lf//10**(le-100)
return(int(msqrt(a))*10**((le-100)//2))
#looping function
def lsqrt(v,seed=sqNear0):
assert(v>=0)
if v<1:
return(0L)
r=seed(v)
r=min(r,v//r)
while (r+1)**2<v:
r+=int((v//r+1-r)/2)
r=min(r,v//r)
return(r)