Guest User

Untitled

a guest
Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. def common_divisors_generator(n, m):
  2.  
  3. # Init code
  4. factors_n = [i for i in range(1, n + 1) if n%i == 0]
  5. factors_m = [i for i in range(1, m + 1) if m%i == 0]
  6.  
  7. # Recursive code
  8. for fn in factors_n:
  9. for fm in factors_m:
  10. if fn == fm:
  11. yield fn
  12.  
  13. # The next line is fast because no code is executed yet
  14. cdg = common_divisors_generator(1537745, 373625435)
  15. # Next line is slow because init code is executed on first iteration call
  16. for g in cdg:
  17. print(g)
  18.  
  19. class CommonDivisorsIterator(object):
  20.  
  21. def __init__(self, n, m):
  22. # Init code
  23. self.factors_n = [i for i in range(1, n + 1) if n%i == 0]
  24. self.factors_m = [i for i in range(1, m + 1) if m%i == 0]
  25.  
  26. def __iter__(self):
  27. return self
  28.  
  29. def __next__(self):
  30. # Some Pythonic implementation of the recursive code above
  31. # ...
  32. return next_common_divisor
Add Comment
Please, Sign In to add comment