0

I tried to create a generator, that returns the proper divisors of n (not including n itself). It works fine until 256, but starts including n starting from 257 ... I could not figure out why. Thanks for your help!

def divisorGenerator(n):
    large_divisors = []
    y = int(math.sqrt(n))
    for i in xrange(1, y + 1):
        if n % i is 0:
            yield i
            if i is not n / i and n is not n / i:   
                large_divisors.insert(0, n / i)
    for divisor in large_divisors:
        yield divisor
fixxxer
  • 15,568
  • 15
  • 58
  • 76
sputniza
  • 31
  • 1
  • 7
  • ok, thanks! obviously changing 'is not' to '!=' made the difference. Anybody, who can explain to me why 256 or 257 made a difference before? – sputniza May 22 '15 at 10:18
  • 2
    from the link by @ŁukaszR: "In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work", see also http://stackoverflow.com/questions/306313/pythons-is-operator-behaves-unexpectedly-with-integers which links to http://docs.python.org/c-api/int.html – serv-inc May 22 '15 at 10:33

0 Answers0