This code will generate the different sets created:
def gcd(a, b):
return max(x for x in range(1, min(a, b) + 1) if a % x == 0 and b % x == 0)
def get_one(x, y): return y
def foo(k, n):
for i in itertools.groupby(sorted(((x, x%gcd(k, n)) for x in range(1, n+1)), key=get_one, key=get_one):
print(" ".join(str(x) for x, y in i[1]))
For instance for (10, 2):
2 4 6 8 10
1 3 5 7 9
The gcd() function has been singled out for clarity. If anyone wonders how it works, I'd gladly rewrite it more intelligibly.