Total energy electronic structure calculations, based on density functional theory or on the more empirical tight binding approach, are generally believed to scale as the cube of the number of electrons. By using the localization property of the high temperature density matrix we present exact deterministic algorithms that scale linearly in one dimension and quadratically in all others. We also introduce a stochastic algorithm that scales linearly with system size. These results hold for metallic and nonmetallic systems and are substantiated by numerical calculations on model systems.