summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/algorithms/log.cc
blob: 1e49a2ebfe80ca3736b34142c9188fadedf34316 (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
#include<iostream>

using namespace std;

double log(double n) {
    if(n <= 2) return 1.0;
    return 1.0 + log(n/2);
}

int log2(double n) {
    int a = 1;

    while(n > 2) {
        n /= 2;
        ++a;
    }

    return a;
}

int main() {
    for(int i = 0; i < 25; ++i)
        cout << i << ' ' << log(i) << ' ' << log2(i) << endl;
    return 0;
}