Skip to content
Advertisement

Python 3 – Gaussian divisors of a Gaussian integer

I’m trying to write a method to generate a sequence of Gaussian divisors of a Gaussian integer – a Gaussian integer is either a normal integer or a complex number g = a + bi where a and b are both integers, and a Gaussian divisor of a Gaussian integer g is a Gaussian integer d such that g / d is also a Gaussian integer.

I’ve got the following code.

JavaScript

It seems to “mostly” work but for some inputs it is missing out some Gaussian divisors, e.g. for 2 I would expect the sequence to include the divisor -2 + 0j (which is just -2), but it is missing. I can’t figure out why it is doing this or where there is gap in the logic.

JavaScript

Advertisement

Answer

Instead of just yielding

JavaScript

you could additionally

JavaScript

Because your loops start and stop at int(math.sqrt(abs(g))) = int(sqrt(2)) which is just 1 so it will just test -1, 0 and 1.

Alternativly if you want to include -2 and 2 in your loops you need to either increment the ubound or math.ceil the sqrt result.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement