problems with code

cancal cancal328 at shaw.ca
Wed May 21 20:04:53 EDT 2003


This script finds "Smith numbers" in the range passed to smith(). (Smith 
numbers are numbers the sum of whose digits equals the sum of digits in 
their prime factors.)

At runtime a type error is raised: interation over non-sequence, at the 
line 'for each in flist' in the function pf(). pfactors() is a 
home-grown function that returns a list of the prime factors of its 
argument. It is working correctly and returning a valid list -- trust 
me. PF() WORKS CORRECTLY WHEN IT IS CALLED BY ITSELF. The purpose of 
pf() is to add to the list of prime factors repeated instances of the 
same factor when this is necessary for the product of all the factors to 
equal the number.

Is there anything obviously, syntactically wrong with this code? I've 
been tearing my hair over this for two days now. Any help would be 
greatly appreciated. It's version 2.2 running in the PythonWin 
environment. Here's the code:

from math1 import pfactors

def pf(n):
     fprod = 1
     flist = pfactors(n)
     for each in flist:
         fprod *= each
     while 1:
         if fprod == n:
             break
         else:
             f = n / fprod
             flist.append(f)
             fprod *= f

     return flist

def smith(start, end):
     for i in range(start, end):
         factorstring = ""
         flist = pf(i)
         numbersum = 0
         factorsum = 0

         if flist:
             for j in flist:
                 factorstring += str(j)
             for m in factorstring:
                 factorsum += int(m)
             number = str(i)
             for k in number:
                 numbersum += int(k)
             if factorsum == numbersum:
                 print i, "is a Smith number"





More information about the Python-list mailing list