Any way faster than pow() to compute an integer power of 10 in C++?

后端 未结 12 917
逝去的感伤
逝去的感伤 2020-12-05 13:25

I know power of 2 can be implemented using << operator. What about power of 10? Like 10^5? Is there any way faster than pow(10,5) in C++? It is a pretty straight-forw

12条回答
  •  忘掉有多难
    2020-12-05 13:38

    No multiplication and no table version:

    //Nx10^n
    int Npow10(int N, int n){
      N <<= n;
      while(n--) N += N << 2;
      return N;
    }
    

提交回复
热议问题