The code:
total = 0
for number in xrange(10000):
divisors = 0
divisors2 = 0
for dividend in xrange(1, number/2):
if number % dividend == 0:
divisors = divisors + dividend
for dividend2 in xrange(1, divisors/2):
if divisors % dividend2 == 0:
divisors2 = divisors2 + dividend2
if number == divisors2:
total = total + number + divisors
print total
The code is supposed to generate amicable numbers(i.e. number that total number of divisors less than itself equal another number that's total number of divisors equal the original number, see Project Euler, problem 21) under 10,000 and add them as it finds them. It's generating 48, which is way too low.
The program ran a lot faster than I expected it to: I'm running through a lot of numbers and I know for a fact that this isn't a very fast way to get the proper divisors, so I suspected something was up with the loop, either that Python was just stopping unexpectedly, or was running the loops out of order. If I put a command to print divisors before the beginning of the next loop, it goes on forever, and tends to print long lines of the same number. Something strange is definitely going on here. I googled "strange loop behavior", and searched here, to no avail. I also checked [here].2
What's going on and what should I do about it?
Thank you in advance.