Skip to content

Instantly share code, notes, and snippets.

@vijayanandrp
Created June 14, 2017 21:34
Show Gist options
  • Save vijayanandrp/c55228e5591ae598176eab8ed621e3cb to your computer and use it in GitHub Desktop.
Save vijayanandrp/c55228e5591ae598176eab8ed621e3cb to your computer and use it in GitHub Desktop.
Simple python code to solve Factorization Problems - Fermat Theorem
import math
def gcd(a, b):
while a != b:
if a > b:
a = a - b
else:
b = b - a
return a
n = 80646413
a = int(math.floor(math.sqrt(n)))
b = 0
while(True):
b = a*a - n
t = b**(1/2)
if b == t*t:
b = t
break
a = a+1
print 'the factor are ',gcd(a+b,n),gcd(a-b,n)
# u^2 - v^2 = n
# (u+v)(u-v) = n
# to find 'u' take floor(sqrt(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment