frustrated stupid newbie question

Dave Reed dreed at capital.edu
Tue Mar 12 18:17:42 EST 2002


> From: "Scott Kurland" <skurland at juggler.net>
> 
> Why isn't this $%^*#$% program working?
> 
> #Searching for perfect numbers
> 
> howhigh= input ("How high should I check?")
> for number in range (1,howhigh):
>  factorsum = 0
>  halfnumber=number/2
>  for checking in range (1,halfnumber):
>   if number/checking == int (number/checking):
>    factorsum = factorsum + checking
>  if number == factorsum:
>   print number
> 
> 
> 
> Flame away, I can't feel dumber than this.


Two problems I see:

number / checking does integer arithmetic. Try starting up the python
interpreter and type: 5 / 2

Use:
if number % checking == 0:
this determines if the remainder is zero.

Also, you need to go one past the half number since 2 is a factor for
even numbers so you'll want the other factor. Again, with the python
interpreter, type: range(1, 10)

Try this:

howhigh= input ("How high should I check?")
for number in range (1,howhigh):
   factorsum = 0
   halfnumber= number / 2 + 1
   for checking in range (1,halfnumber):
      if number % checking == 0:
         factorsum = factorsum + checking
   if number == factorsum:
      print number

Also, see:

http://home1.pacific.net.sg/~novelway/MEW2/lesson1.html

Dave




More information about the Python-list mailing list