Advertisement
Programmin-in-Python

Divisibility tests

Jan 25th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def divisibility(b,d):
  2.     type2, type3, type6, type7, type11 = (False,)*5
  3.     num, no_to_digits = 0, 0
  4.  
  5.     if b == 2:
  6.         num = bin(d)
  7.     elif b == 8:
  8.         num = oct(d)
  9.     elif b == 16:
  10.         num = hex(d)
  11.     else:
  12.         finalNum = int(str(d),b)
  13.  
  14.     if isinstance(num, str):
  15.         finalNum = int(num[2:])
  16.        
  17.     if (finalNum%2 == 0) or (finalNum%4 == 0) or (finalNum%5 == 0) or (finalNum%8 == 0) or (finalNum%10 == 0):
  18.         type2 = True
  19.  
  20.         if (finalNum%2 == 0) or (finalNum%5 == 0) or (finalNum%10 == 0):
  21.             no_to_digits = 1
  22.         elif (finalNum%4 == 0):
  23.             no_to_digits = 2
  24.         elif (finalNum%8 == 0):
  25.             no_to_digits = 3
  26.  
  27.     if (finalNum%3 == 0) or (finalNum%9 == 0):
  28.         type3 = True
  29.     if (finalNum%11 == 0):
  30.         type11 = True
  31.     if (finalNum%6 == 0):
  32.         type6 = True
  33.     if (finalNum%7 == 0):
  34.         type7 = True
  35.  
  36.     if type2:
  37.         return f"2-type\n{no_to_digits}"
  38.     elif type3:
  39.         return "3-type"
  40.     elif type6:
  41.         return "6-type"
  42.     elif type7:
  43.         return "7-type"
  44.     elif type11:
  45.         return "11-type"
  46.  
  47. var = divisibility(2,3)
  48. print(var)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement