Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def common_divisors_generator(n, m):
- # Init code
- factors_n = [i for i in range(1, n + 1) if n%i == 0]
- factors_m = [i for i in range(1, m + 1) if m%i == 0]
- # Recursive code
- for fn in factors_n:
- for fm in factors_m:
- if fn == fm:
- yield fn
- # The next line is fast because no code is executed yet
- cdg = common_divisors_generator(1537745, 373625435)
- # Next line is slow because init code is executed on first iteration call
- for g in cdg:
- print(g)
- class CommonDivisorsIterator(object):
- def __init__(self, n, m):
- # Init code
- self.factors_n = [i for i in range(1, n + 1) if n%i == 0]
- self.factors_m = [i for i in range(1, m + 1) if m%i == 0]
- def __iter__(self):
- return self
- def __next__(self):
- # Some Pythonic implementation of the recursive code above
- # ...
- return next_common_divisor
Add Comment
Please, Sign In to add comment