Rewrite getting groups with GCD