Member Avatar for leegeorg07

hi again! at school we were learning about perfect numbers and i thought it would be useful to write a program to find them i currently have this code:

b= 1
h= 1
factors = [1,]
n = float(raw_input("what is the number?"))

while h < n:
        if h*b == n:
                print h, "is not a factor"
                h=h+1
        else:
                if h == 1:
                        print h, "is a factor"
                        h=h+1
                else:
                        print h, "is a factor"
                        factors.append(h)
                        h=h+1


if h <= n:
        b = factors [0:] + factors [:40]
        c = factors [0:] * factors [:40]
        if b==n:
                print n, "is not a perfect number"
        else:
                print "halfway there"
        if c==n:
                print n, "is not a perfect number"
        else:
                print n, "is a perfect number"

but i get the error

Traceback (most recent call last):
File "C:\Python25\perfect nums.py", line 22, in <module>
c = factors [0:] * factors [:40]
TypeError: can't multiply sequence by non-int of type 'list'

can you help please?

thanks in advance

You might want to rethink what the line c = factors [0:] * factors [:40] is doing...

Look at this example for what happens using * on lists:

>>> [ 1, 2, 3 ] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> [ 1, 2, 3 ] * [ 2, 3 ]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'list'
Member Avatar for leegeorg07

ok can you give me any advice on how to change it to multiply all of the numbers in the list together?

>>> L = range(1, 10)
>>> L
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> reduce(int.__mul__, L)
362880
>>>

but there are other problems in your program :) for example float should be int . Also, what is b ?

>>> reduce(int.__mul__, L)
362880
>>>

Wow, I really like that.. never used reduce.

A suggestion for the OP: so that the above isn't so cryptic maybe it'll be easier to wrap your head around in this form:

>>> L = range(1,10)
>>> reduce(lambda x, y: x*y, L)
362880
>>>
Member Avatar for leegeorg07

hi ive just found out that is doesnt work by using

b= 1
h= 1
factors = [1,]
n = float(raw_input("what is the number?"))

while h < n:
        if h*b == n:
                print h, "is not a factor"
                h=h+1
        else:
                if h == 1:
                        print h, "is a factor"
                        h=h+1
                else:
                        print h, "is a factor"
                        factors.append(h)
                        h=h+1


if h <= n:
        for i in factors:
                a=i
                print a
        b = factors [0:] + factors [:40]
        if b==n:
                print n, "is not a perfect number"
        else:
                print "halfway there"
        if b==n:
                print n, "is not a perfect number"
        else:
                print n, "is a perfect number"

you can see that all the numbers smaller than 6 are thought of as a factor i now need lots of help :(

Yes you should figure out the factors portion of your code first... one obvious problem might be that you never change b? So you're always multiplying h*1 ... and secondly, if h*b == n; wouldn't that make h and b both factors of n ??

You can do something like:

inp = user input
for i in (1, inp+1):
    for j in (i, 1, -1):
        if i*j == inp:
            i and j are factors
return factors
Member Avatar for leegeorg07

i think i know the simplest way of doing this which is to first find the factors like i tried but is there maybe some way of 'factorizing' a number in any of the available modules?

I don't know if there is a module to simply find factors, but it is such a simple operation that you can make your own function... from the psuedo-code I posted eralier:

>>> def get_factors( inp ):
...     ret = [ 1, inp ]
...     for i in xrange( inp + 1 ):
...         for j in xrange( i, 1, -1 ):
...             if i * j == inp:
...                 ret += [ i, j ]
...     ret.sort()
...     return ret
...     
>>> get_factors(6)
[1, 2, 3, 6]

I think the way to go about this is to use modulo or divmod to find the numbers that you can divide the given number by. That is the remainder == 0. You will be working with single numbers which is less confusing. Actually, you can use the (given number / 2) since all numbers greater than the halfway point can't be divided into to the number. Then sum these numbers and see if they add up to the given number.

I played around with a function that returns a list of factors a few months ago. You only need to check numbers up to sqrt(number).

def factors(num):
    outList = [i for i in xrange(1, int(num**0.5)+1) if not num%i]
    if outList:
        outList.extend([num/outList[i] for i in range(len(outList)-[1, 2][outList[-1]**2 == num or 0], -1, -1)])
    return outList

Example:

>>> factors(14564766333)
[1, 3, 13, 39, 1153, 3459, 14989, 44967, 323899L, 971697L, 4210687L, 12632061L, 373455547L, 1120366641L, 4854922111L, 14564766333L]
>>>

plz some comments about the code

def factors(num):
outList =
if outList:
outList.extend([num/outList for i in range(len(outList)-[1, 2][outList[-1]**2 == num or 0], -1, -1)])
return outList

number = int(raw_input("plz enter the number : "))
print factors(number)
outList = factors(number)
result = 0
for x in range (len(outList)):
result += outList[x]
if ((result / 2) == number):
print "the number if perfect"
else:
print "the number isn't perfect"

for number in range (1000):
outList = factors(number)
result = 0
for x in range (len(outList)):
result += outList[x]
if ((result / 2) == number):
print "the %2d is perfect" %(number)
else:
print "the %2d isn't perfect" % (number)

plz some code tags about the code:

[code=python] # Put code inside here

[/code]

This will make your code readable on this forum. Also, as per forum guidelines, if you're asking a new question start a new thread, don't go digging up old threads with a similar topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.