fork(10) download
  1. # pollard p-1
  2.  
  3. from fractions import gcd
  4.  
  5. def primes(n):
  6. ps = []
  7. sieve = [True] * n
  8. for p in range(2,n):
  9. if sieve[p]:
  10. ps.append(p)
  11. for i in range(p*p,n,p):
  12. sieve[i] = False
  13. return ps
  14.  
  15. def pminus1(n, b):
  16. c = 2
  17. for p in primes(b):
  18. pp = p
  19. while pp < b:
  20. c = pow(c, p, n)
  21. pp = pp * p
  22. g = gcd(c-1, n)
  23. if 1 < g < n:
  24. return g
  25. return 0
  26.  
  27. print primes(100)
  28. print pminus1(87463, 120)
Success #stdin #stdout 0.19s 12464KB
stdin
Standard input is empty
stdout
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
149