Python Forum

Full Version: New to python, having trouble with an exercise
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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_list
It's not returning anyhting like what im being asked for. Im new to python and programming. Thanks in advance!
I don't know if I solved it but this will get you a little closer.

import math # you need to import the math module used on line 14.

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([int(number), sum_of_divisors2])# make number an integer.
 
    print(final_list)# print instead of return

list_squared(2, 6) # this runs the function with your choice of numbers (n, m).
output:
[[2, 1], [4, 49], [4, 64], [5, 100]]
Mostly the errors are in the indentation - making the loops not end where they should, and not zeroing out values when they should. This works.
import math
def list_squared(m, n):
    final_list = []
    for number in range(m, n+1):
        divisors_list = []
        for i in range(1, number + 1):
            if number % i == 0:
                divisors_list.append(i)
        sum_of_divisors2 = 0
        for divisor in divisors_list:
            divisors_squared = divisor ** 2
            sum_of_divisors2 += divisors_squared
        if math.sqrt(sum_of_divisors2).is_integer() :
            final_list.append([number, sum_of_divisors2])
 
    return final_list
print(list_squared(5,1000))
Thank you so much! I've still got a lot to learn