0

I am trying to find perfect number with using a function in python but I want to do this without using any parameters in function and using double for loop. I wonder that if it is possible. My example code is under the below. I would appreciate it if you help.

def perfectnumber():
  sum = 0
  #These numbers going to be our numbers which will be divided.
  for j in range(1,1001):
      #These numbers going to be our numbers which will divide first loop numbers(j)
      for k in range(1,1001): 
Carlos Gonzalez
  • 858
  • 13
  • 23
Cheef
  • 1
  • 1

1 Answers1

0
import math
def divisors(n):
    divs = [1]
    for i in range(2,int(math.sqrt(n))+1):
        if n%i == 0:
            divs.extend([i,n/i])
    return list(set(divs))


def is_perfect_number(n):
    return n == sum(divisors(n))

res = []
for i in range(2,1001):
    if is_perfect_number(i):
        res.append(i)

print(res)
  • Thank you so much. I am really appreciate it. Even though this is not the exact solution about my question, your answer may be another solution of my question. – Cheef Jun 20 '19 at 06:44
  • :) would you plz tell me what's your exact answer? I can edit my answer for future references – alireza yazdandoost Jun 20 '19 at 07:17
  • There is no problem with your code. I have just wondered if there is any way to create only one function and using double foor loop in this function for finding 1-1000 perfect numbers. – Cheef Jun 20 '19 at 09:03