If I understood correctly, this works only if `primitive_root(p1, all_roots)` is odd. That's why `primitive_root(82)` currently returns a wrong answer. It returns `primitive_root(41)` which is 6.