ashpatel

Number of common divisors of 2 numbers

Jan 4th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.38 KB | None | 0 0
  1. #Finding the number of common divisors of 2 numbers.
  2.  
  3. def gcd(a, b):
  4.     if b > 0:
  5.         return gcd(b, a%b)
  6.     else:
  7.         return a
  8.  
  9.  
  10. a, b = map(int, input().split())
  11. if b > a:
  12.     a, b = b, a
  13. n = gcd(a, b)
  14. ans = 0
  15. sqt = int(n**0.5)
  16. i = 1
  17. while i <= sqt:
  18.     if n % i == 0:
  19.           ans += 2
  20.           if i == n//i:
  21.               ans -= 1
  22.     i += 1
  23. print(ans)
Add Comment
Please, Sign In to add comment