P417: Optimized Java solution using number theory, but removed explanation.
P417: Optimized Java solution.
P431: Added Java solution.