A rapidly converging algorithm for solving the Kohn—Sham and related equations in electronic structure theory