Solve the following problems using Python on your computer. You may not use GenAI to do so, and you must use programming to solve the problems-- do not solve by hand.
Once you solve the three problems, use the answers that are printed out as a lockbox code. Go to the front of the classroom and use this code on the lockbox. If people are waiting, you only have 1 try before going to the back of the line again.
Write a function that takes in an integer (n) and calculates a sequence such that the next number is the sum of the square of the previous number's digits (i.e. if n = 157, the next number will be 1^2+5^2+7^2 = 1 + 25 + 49 = 75). Repeat this process until n becomes a single-digit number.
RETURN the single-digit number.
Then run your function with an input of n=63946.
def sum_of_squared_digits(n):
next_value = n
counter = 0
while #REPLACE WITH YOUR CODE: # Continue until next_value becomes a single digit
#REPLACE WITH YOUR CODE to get the sum of the squares of digits as the next_value
#Hint: You can convert the number to a string to access individual digits
#Hint 2: You might want to have a break statement while testing code so it doesn't run forever (if the code isn't right on the first try).
# This is what the counter is for. You can remove it from the final code.
counter += 1
if counter > 100:
break
return next_value
sum_of_squared_digits(63946)
Write a function that takes in a list (l) as an argument and calculates a total by going through the items in the list and:
RETURN the total.
Then run your function with an input of l = [4,6,2,8,4,6,9,3,1,1,1,2,5,4,9,7,12,15,3,0,2,3,9,6,5,10,14,19,15,9,4]
def calculate_total(lst):
total = 0
for num in lst:
if #REPLACE WITH YOUR CODE:
#REPLACE WITH YOUR CODE
elif #REPLACE WITH YOUR CODE:
#REPLACE WITH YOUR CODE
return total
l =[4,6,2,8,4,6,9,3,1,1,1,2,5,4,9,7,12,15,3,0,2,3,9,6,5,10,14,19,15,9,4]
calculate_total(l)
RETURN the first digit that is less than 10 in the sequence.
Then run your function with an input of n = 703.
def collatz_sequence(n):
sequence = []
next_number = n
while #REPLACE WITH YOUR CODE: # Continue until next_number becomes a single digit
if #REPLACE WITH YOUR CODE:
#REPLACE WITH YOUR CODE to update the next_number variable accordingly
else:
#REPLACE WITH YOUR CODE to update the next_number variable accordingly
return next_number
collatz_sequence(703)