# Python code for Pollard p-1 # factorization Method # importing "math" for # calculating GCD import math # importing "sympy" for # checking prime import sympy # function to generate # prime factors def pollard(n): # defining base a = 2 # defining exponent i = 2 # iterate till a prime factor is obtained while(True): # recomputing a as required a = (a**i) % n # finding gcd of a-1 and n # using math function d = math.gcd((a-1), n) # check if factor obtained if (d > 1): #return the factor return d break # else increase exponent by one # for next round i += 1 # Driver code n = 1403 # temporarily storing n num = n # list for storing prime factors ans = [] # iterated till all prime factors # are obtained while(True): # function call d = pollard(num) # add obtained factor to list ans.append(d) # reduce n r = int(num/d) # check for prime using sympy if(sympy.isprime(r)): # both prime factors obtained ans.append(r) break # reduced n is not prime, so repeat else: num = r # print the result print("Prime factors of", n, "are", *ans)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question