Question 1[2 marks]
For this question, we are looking at an algorithm used to implement the divisibility rule for 3 which states that:Let’s look at the following example: is 378 divisible by 3? Here is the Python code for our recursive algorithm:
def isDivisibleByThree(n): sum = 0 for digit in str(n): sum = sum + int(digit) if sum>=10: return isDivisibleByThree(sum) else: if sum==3 or sum==6 or sum==9: return True else: return False number = int(input("Type a number")) if isDivisibleByThree(number)==True: print("This number is divisible by 3.") else: print("This number is not divisible by 3.")Explain why the above code can be described as being a recursive algorithm? State on which line of this code does the recursion occur?
Question 2[4 marks]
Trace this algorithm if the user enters the value 8479
Question 3[4 marks]
Rewrite the function isDivisibleByThree() using an iterative approach instead of a recursive approach.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area