Divisibility of numbers

# Source: "The mathematics of everyday life" # by Alfred S. Posamentier and Christian Spreitzer # Helpers def digit_list(number): return list(map(int, list(str(number)))) def merge_digits(numbers): return int(''.join(map(str, numbers))) def divide_by_7_13_17(number, divisor, factor): diff = float('inf') while not diff < 100: digits = digit_list(number) last = digits[-1] digits.pop() diff = merge_digits(digits) - factor*last number = diff return not diff % divisor # Divisibility def divisible_by_2(number): return not digit_list(number)[-1] % 2 def divisible_by_3(number): return not sum(digit_list(number)) % 3 def divisible_by_4(number): return not merge_digits(digit_list(number)[-2:]) % 4 def divisible_by_5(number): return digit_list(number)[-1] in [0, 5] def divisible_by_6(number): return divisible_by_2(number) and divisible_by_3(number) def divisible_by_7(number): return divide_by_7_13_17(number, 7, 2) def divisible_by_8(number): return not merge_digits(digit_list(number)[-3:]) % 8 def divisible_by_9(number): return not sum(digit_list(number)) % 9 def divisible_by_10(number): return digit_list(number)[-1] == 0 def divisible_by_11(number): sum_at_odd_even_indices = [0, 0] for idx, d in enumerate(digit_list(number)): sum_at_odd_even_indices[~idx & 1] += d odd_sum, even_sum = sum_at_odd_even_indices return not (odd_sum - even_sum) % 11 def divisible_by_13(number): return divide_by_7_13_17(number, 13, 9) def divisible_by_17(number): return divide_by_7_13_17(number, 17, 5) def divisible_by_25(number): return not merge_digits(digit_list(number)[-2:]) % 25 # Test print( divisible_by_2(40), divisible_by_2(41), divisible_by_3(741), divisible_by_3(742), divisible_by_4(1464), divisible_by_4(1465), divisible_by_5(34534535), divisible_by_5(34534536), divisible_by_6(707718), divisible_by_6(707719), divisible_by_7(68530), divisible_by_7(68531), divisible_by_8(12552), divisible_by_8(12553), divisible_by_9(101277), divisible_by_9(101278), divisible_by_10(9880), divisible_by_10(9881), divisible_by_11(1716), divisible_by_11(1717), divisible_by_13(6175), divisible_by_13(6176), divisible_by_17(111418), divisible_by_17(111419), divisible_by_25(11325), divisible_by_25(11326), ) # True False True False True False True False True False True False True False True False True False True False True False True False True False