Python Forum
New to python, having trouble with an exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python, having trouble with an exercise
#1
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!
Reply
#2
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]]
Reply
#3
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))
Reply
#4
Thank you so much! I've still got a lot to learn
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with installing python domingo251 2 590 Sep-23-2023, 12:03 AM
Last Post: ICanIBB
  Headfirst Python Exercise GeorgeSears 3 1,375 Jun-11-2022, 06:36 PM
Last Post: GeorgeSears
  Wrong code in Python exercise MaartenRo 2 1,521 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  Python Exercise Doubt azure 4 2,661 Apr-21-2020, 01:15 PM
Last Post: azure
  More Python Embedding Trouble jibarra 3 2,907 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Trouble installing Python 3.5 (.4 and .0) GKK 1 2,317 Aug-17-2018, 11:34 AM
Last Post: Larz60+
  Exercise 20, Learn Python 3 the Hard Way Dixon 6 6,011 Feb-26-2018, 04:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020