[Mathematics] fastSum fastPow

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
using namespace std;

int fastSum(int n){
if(n==1) return 1;
if(n&1==1) return fastSum(n-1) + n;
return (fastSum(n>>1)<<1) + (n>>1)*(n>>1);
}
int fastPow(int n, int p){
if(p==1) return n;
if(p&1==1) return fastPow(n,p-1) * n;
int half = fastPow(n,p>>1);
return half*half;
}
int main(int argc, char** argv){
int NUM = 10;
int N = 2, P = 10;
printf("fastSum %d = %d\n",NUM,fastSum(NUM));
printf("fastPow %d^%d = %d\n",N,P,fastPow(N,P));
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Algorithm/fastSumfastPow/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.