summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/algorithms/pow.cc
blob: 70f5b434e39bbcdd24a04a97f943766c3cbe8660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>

using namespace std;

int pow(int b, int e) {
    if(e == 0) return 1;
    int a = b;
    int inc = b;
    for(int i = 0; i < e-1; ++i) {
        for(int j = 0; j < b-1; ++j) {
            a+=inc;
        }
        inc = a;
    }
    return a;
}

int mul(int b, int e) {
    if(e == 0) return 0;

    return b+mul(b, e-1);
}

int pow2(int b, int e) {
    if(e == 0) return 1;

    return mul(b, pow(b, e-1));
}

int main() {
    cout << pow(2, 5) << endl;
    cout << pow2(2, 5) << endl;
    return 0;
}