-4

Please could you help correct this code! It's for finding all perfect numbers below the set limit of 10,000, I've used comments to explain what I'm doing. Thanks!

#Stores list of factors and first perfect number
facs = []
x = 1

#Defines function for finding factors
def findfactors(num):

    #Creates for loop to find all factors of num
    for i in range(1, num + 1):
        if num % i == 0:

        #Stores factor in facs
        facs.append(i)


#Activates loop
while x < 10000:

    #Finds factors of x and appends them to list
    findfactors(x)

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:

        #Ouputs and increases x
        print(x)
        x += 1

    else:

        #Increases x
        x += 1
coder2305
  • 11
  • 1
  • 5
  • 3
    Would you mind expanding the question with the following: what do you expect the code to do and (more importantly) what is it doing instead? Does it produce error messages and if so: include those in your question. – meissner_ Oct 24 '18 at 13:21
  • How about first checking if the number is even or odd and add a step argument in the range will remove half of the unrequired values as it is not possible to have even factor for an odd number. Just an idea. – mad_ Oct 24 '18 at 13:25
  • 1
    @meissner_ I expect the code to return all perfect numbers below 10,000 but it is only printing '1' which I set x to originally. There are no error messages. Sorry, I'm new to stack overflow! – coder2305 Oct 24 '18 at 13:26
  • @mad_ I'm afraid I have no idea what that means as I am not an expert at all! – coder2305 Oct 24 '18 at 13:27
  • In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). The first perfect number is 6. The next perfect number is 28. This is followed by the perfect numbers 496 and 8128. ([Wikipedia](https://en.wikipedia.org/wiki/Perfect_number)) So, you have to exclude the number itself, but to include 1. – kostic017 Oct 24 '18 at 13:32
  • @coder2305: hhm, that might have something to do with your function, below the line `if num % i == 0:` you're adding things to the facs list but the append-line should probably be indented 4 spaces to the right (moving it into the if "block"), otherwise you're adding all the things to `facs`. – meissner_ Oct 24 '18 at 13:33
  • @meissner_ Yes sorry that was just a copy&pasting error from IDLE – coder2305 Oct 24 '18 at 13:41

4 Answers4

1

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). The first perfect number is 6. The next perfect number is 28. This is followed by the perfect numbers 496 and 8128. (Wikipedia)

You have to exclude the number itself from the factors list.

Also, for each x you have to start with the empty facs and then append to it. You don't want previous numbers factors in that list. The following code works.

x = 1

def findfactors(num):
    facs = []
    for i in range(1, num):
        if num % i == 0:
            facs.append(i)
    return facs


while x < 10000:
    facs = findfactors(x)
    facsum = sum(facs)
    if facsum == x:
        print(x)
        x += 1
    else:
        x += 1
kostic017
  • 356
  • 3
  • 12
0

intialize list inside the def and return and the range should not include the original num so range would be from 1 to num which includes 1 but excludes orginal num so it will generate range from 1 to num-1

#Stores list of factors and first perfect number
x = 1
#Defines function for finding factors
def findfactors(num):
    facs = []
    for i in range(1, num):
        if num % i == 0:
            #Stores factor in facs
            facs.append(i)

    return facs

#Activates loop
while x < 1000:

    #Finds factors of x and appends them to list
    facs=findfactors(x)

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:
        #Ouputs and increases x
        print(x)
    x+= 1

Much faster than generating list Approach from What is the most efficient way of finding all the factors of a number in Python?

#Stores list of factors and first perfect number
x = 1
#Defines function for finding factors
from functools import reduce

def factors(n):
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

#Activates loop
while x < 10000:
    #Finds factors of x and appends them to list
    facs=factors(x)
    facs.remove(x) # remove original number as it is not required further

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:
        #Ouputs and increases x
        print(x)
    x+= 1
mad_
  • 8,121
  • 2
  • 25
  • 40
0

This is a simpler implementation of your logic.

x = 1

#Defines function for finding factors
def isperfectnum(num):
sum=0
    #Creates for loop to find all factors of num
    for i in range(1, num ):
        if num % i == 0:
           sum=sum+i
    if sum==num:
        return TRUE
    else 
        return FALSE

#Activates loop

while x < 10000:

    #Finds perfect numbers
    if isperfectnum(x):
       print(x)
    x=x+1
0

Found the problem!

Should have been:

for i in range(1, num - 1)

Rather than:

for i in range(1, num + 1)

Thanks to all contributors!

coder2305
  • 11
  • 1
  • 5