The divisibility test for 11 states if the difference between the sum of odd digits and the sum of even digits in a number is divisible by 11, the entire number is said to be divisible by 11.
For example, 7689, whereby the sum of odd digits = 9 + 6 = 15, while sum of even digits = 8 + 7 = 15.
As (15 - 15) % 11 == 0, the number is divisible by 11.
I wrote an iterative function to evaluate this, giving a result of True if the number is indeed divisible by 11. How do I write a recursive version of this code? I have trouble breaking down this problem.
def divisible_by_11_iter(num):
num_lst = [i for i in str(num)]
even_sum = 0
odd_sum = 0
for even in num_lst[::2]: # naming does not really matter as negatives taken into acc
even_sum += int(even)
for odd in num_lst[1::2]:
odd_sum += int(odd)
if (odd_sum - even_sum) % 11 == 0:
return True
else:
return False