Ad

This works for all whole numbers, without using the modulus operator.

Code
Diff
  • def divisibility_by_3(x):
    
        x = abs(x)
        while x > 9:
            list_of_values = [int(d) for d in str(x)]
            x = sum(list_of_values)
        return x in [0, 3, 6, 9]
    • def divisibility_by_3(x):
    • list_of_values = [int(d) for d in str(x)]
    • summation = sum(list_of_values)
    • if summation % 3 == 0:
    • return True
    • else:
    • return False
    • x = abs(x)
    • while x > 9:
    • list_of_values = [int(d) for d in str(x)]
    • x = sum(list_of_values)
    • return x in [0, 3, 6, 9]