I'm working on a exercise, which has a time to compile limit. Im not looking for any other way to solve this problem, but an explanation. Why is this code so slow? 0.001s it's quite high isn't it? Is there any mistake?
Though I think Peter87 is right, within the timed section is a cout statement. I expect the cout itself is much slower than any of the other code. In any calculation-intensive code, I would avoid input/output completely, store the result somewhere else instead, and defer the i/o until after calculation is complete.