summaryrefslogtreecommitdiff
path: root/Year_1/Programming_2/algorithms/cbrt.cc
blob: 30c7792ab79927cfe16b96cca60f8ff178d18772 (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
35
36
37
#include<iostream>

using namespace std;

int cbrt(int n) {
    int i = 0;
    int j = n;
    int q = (i+j)/2;
    while(q*q*q != n) {
        q = (i+j)/2;
        if(q*q*q < n)
            i=q;
        else
            j=q;
    }
    return q;
}

double cbrt2(int n) {
    int i = 1;
    while(i*i*i <= n)
        ++i;
    // precision
    
    --i;
    while(i*i*i < n)
        i+=0.000001;
    
    return i;
}

int main() {
    int i;
    cin >> i;
    cout << i << ' ' << cbrt(i) << endl;
    return 0;
}