Skip to content

Instantly share code, notes, and snippets.

@corehello
Last active September 29, 2021 15:31
Show Gist options
  • Save corehello/407302c41b54d3638535ee87c199b3b0 to your computer and use it in GitHub Desktop.
Save corehello/407302c41b54d3638535ee87c199b3b0 to your computer and use it in GitHub Desktop.
3th root calculation proof of concept
def sqrt3(n, p):
# any number can be writen as n = a*10 +b, where a >=0, b is 0~9
# for example 123 = 12 * 10 + 3
# so then any number N = (a*10 + b)^3 = 1000*a^3 + 300*a^2*b + 30*a*b^2 + b^3 + remainer
# = 1000*a^3 + b*(300*a^2 + 30*a*b + b^2) + remainer
# then b*(300*a^2 + 30*a*b + b^2) <= N - 1000*a^3
# then we can calulate max b to match the expression
print("sqrt3 {} with {} precision is:".format(n, p))
if len(n) %3 != 0:
n = "0"*(3-len(n)%3) + n
splited_n = [n[i:i+3] for i in range(0,len(n),3)]
A,B,Remainer,Result = 0,0,0,""
def findb(N, a, b, remainer, result):
for j in range(0, 11):
c = remainer*1000 + int(N)
if j*(300*a*a + 30*a*j +j*j) > c:
b = j-1
remainer = c - b*(300*a*a + 30*a*b +b*b)
a = 10*a + b
result = "{}{}".format(result, b)
break
return a,b,remainer,result
for i in splited_n:
A,B,Remainer,Result = findb(i, A, B, Remainer, Result)
if Remainer != 0:
Result = "{}.".format(Result)
for i in ["000" for i in range(p)]:
A,B,Remainer,Result = findb(i, A, B, Remainer, Result)
return Result,A,Remainer
@corehello
Copy link
Author

> sqrt3("1117", 100)
sqrt3 1117 with 100 precision is:
('10.3757076016152723443705022336612204438590687561861136537248724453317971388480542367950056479536171279',
 103757076016152723443705022336612204438590687561861136537248724453317971388480542367950056479536171279,
 10191420830713470219904633075149898570003885616411667881081123698032858862717609432433416151366991579589177045691626477193310002052568741404591177505785830347460033179729326465418175863355326445000849361)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment