roman_gemini

Common Fractions

Jun 22nd, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. from functools import reduce
  2. from math import sqrt
  3.  
  4.  
  5. def is_fraction(of):
  6.     return lambda n: of % n == 0
  7.  
  8.  
  9. def fractions(val):
  10.     lst = list(filter(is_fraction(val), range(2, int(sqrt(val)) + 1)))
  11.     return set(lst + list(map(div(val), lst)))
  12.  
  13.  
  14. def intersect(a, b):
  15.     return a.intersection(b)
  16.  
  17.  
  18. def div(n):
  19.     return lambda x: int(n / x)
  20.  
  21.  
  22. def common(*a):
  23.     return sorted(reduce(intersect, [fractions(x) for x in a]))
  24.  
  25.  
  26. print(common(512, 1024))
Advertisement
Add Comment
Please, Sign In to add comment