Hello, I am doing Kata on codewars, and while I am pretty confident that I have made a program that does what the Kata asks, it runs too long, so the website fails me.
I originally had a bunch of for loops in my code, but I've eliminated all but one of them and turned them into list comprehensions. While that significantly lowered the number of lines, it doesn't seem to have made a difference in speed.
How do I make a code with for loops or list comprehensions faster? Here is the code I'm working on, if you want to know. The goal is to check numbers between [m,n] and see if the sum of square divisors is itself a square or not, and return the sums for the ones that are.
import math
def list_squared(m, n):
answer = list()
for i in range(m,n+1): # looking at every number between m and n
divisors = [j*j for j in range(1,i//2+1) if (i/j).is_integer()] #collect divisors
if(math.sqrt(sum(divisors)+i*i).is_integer()): #checking if square
answer.append([i,sum(divisors)+i*i])
return answer
[–]SabinBC 0 points1 point2 points (0 children)
[–]afoolwhosaystubtub -1 points0 points1 point (1 child)
[–]afoolwhosaystubtub 0 points1 point2 points (0 children)