Last active
September 29, 2021 15:31
-
-
Save corehello/407302c41b54d3638535ee87c199b3b0 to your computer and use it in GitHub Desktop.
3th root calculation proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
corehello
commented
Sep 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment