Skip to content

Commit c583c58

Browse files
committed
add lehman algorithm
1 parent edd6238 commit c583c58

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

lehman.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from gmpy2 import *
2+
3+
4+
def cuberoot(n):
5+
a,b = iroot(n,3)
6+
return a
7+
8+
9+
def lehman(n):
10+
"""
11+
based on: https://programmingpraxis.com/2017/08/22/lehmans-factoring-algorithm/
12+
"""
13+
if is_congruent(n, 2, 4):
14+
return []
15+
16+
for k in range(1, cuberoot(n), 1):
17+
nk4 = n*k << 2
18+
ki4 = isqrt(k) << 2
19+
ink4 = isqrt(nk4) + 1
20+
i6,_ = iroot(n, 6)
21+
ink4i6ki4 = ink4 + (i6 // (ki4))
22+
for a in range(ink4, ink4i6ki4 + 1, 2):
23+
b2 = (a * a) - nk4
24+
if is_square(b2):
25+
b = isqrt(b2)
26+
p = gcd(a + b, n)
27+
q = gcd(a - b, n)
28+
return p, q
29+
return []
30+
31+
def tests():
32+
N = [29 * 89, 3*11, 3141592651*3141592661, 7919*10861]
33+
N += [94738740796943840961823530695778701408987757287583492665919730017973847138345511139064596113422435977583856843887008168202003855906708039013487349390571801141407245039011598810542232029634564848797998534872251549660454277336502838185642937637576121533945369150901808833844341421315429263207612372324026271327]
34+
N += [9733382803370256893136109840971590971460094779242334919432347801491641617443615856221168611138933576118196795282443503609663168324106758595642231987246769*9733382803370256893136109840971590971460094779242334919432347801491641617443615856221168611138933576118196795282443503609663168324106758595642231987248907]
35+
N=sorted(N)
36+
for n in N:
37+
p,q = lehman(n)
38+
print(n,p,q)
39+
assert p*q == n
40+
41+
if __name__ == "__main__":
42+
tests()
43+

0 commit comments

Comments
 (0)