Okay, first of all, I will put a code that doesn't relate to what I want, but its basically close to what I want.
def perfect(n):
return n == sum(i for i in range(1, n) if not n % i)
this function is basically perfect number but in one line.
I have a question to make list of all the commons of a number ( if number is 8, then list = [1, 2, 4, 8]. I wanted to try something like that, in one or two line, but I could not really manage to do it... I know how to solve it with a few lines, but I want to try to be better and more efficient in coding.
I tried this:
def common_number(number):
empty_list = []
return empty_list.append() = (common for common in range(1, number + 1)) if number % common == 0
is there any way of making the code efficient like the perfect number, but in the common, in a way it will work? ill be happy to know if there is a way. You can just give me a tip ( its preferrable, not an answer ). Thanks in regards.
EDIT: I know how to solve the code, its an easy question:
def common_number(number):
empty_list = []
for common in range(1, number + 1):
if number % common == 0:
empty_list.append(common)
return empty_list
x = common_number(8)
print(x)
But as said, I want by efficient like I did above ( someone helped me like that )