Feb-17-2020, 06:58 PM
So the exercise gives me two numbers m and n, I need to make a function that iterates through these numbers to find which one has divisors that, when squared, then summed, results in a square.
Here's an example: Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square.
I need to return a list with the resulting numbers, and their respective sum of squared divisors, in this case it would be [42, 2500].
So I wrote this code:
Here's an example: Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square.
I need to return a list with the resulting numbers, and their respective sum of squared divisors, in this case it would be [42, 2500].
So I wrote this code:
def list_squared(m, n): divisors_list = [] sum_of_divisors2 = 0 final_list = [] for number in range(m, n): for i in range(1, number + 1): if number % i == 0: divisors_list.append(i) for divisor in divisors_list: divisors_squared = divisor ** 2 sum_of_divisors2 += divisors_squared if math.sqrt(sum_of_divisors2).is_integer() == True: final_list.append([number, sum_of_divisors2]) return final_listIt's not returning anyhting like what im being asked for. Im new to python and programming. Thanks in advance!