fork(3) download
  1. from math import sqrt
  2.  
  3. def factors0(n): # http://stackoverflow.com/a/16007256/849891
  4. while n > 1:
  5. for i in range(2, n + 1):
  6. if n % i == 0:
  7. n /= i
  8. yield i
  9. break
  10.  
  11. def factors(n): # improve (cf. http://stackoverflow.com/a/15703327/849891)
  12. j = 2
  13. while n > 1:
  14. for i in xrange(j, int(sqrt(n+0.05)) + 1):
  15. if n % i == 0:
  16. n /= i ; j = i
  17. yield i
  18. break
  19. else:
  20. if n > 1:
  21. yield n; break
  22.  
  23. # factors0(9000009): [3,3,101,9901] 0.59s-8.6M
  24. # factors( 9000009): [3,3,101,9901] 0.08s-8.7M
  25.  
  26. for factor in factors(25*27): # 600851475143999917 # [41, 37309, # 0.36s-8.7M
  27. print factor # 392798360393] # 0.36s-8.7M
Success #stdin #stdout 0.08s 8728KB
stdin
Standard input is empty
stdout
3
3
3
5
5