Do you have the rules for doing that? What is a "primitive root"?
Without a set of rules, it is very hard to write a program.