-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisperfectcube.cpp
More file actions
39 lines (31 loc) · 780 Bytes
/
isperfectcube.cpp
File metadata and controls
39 lines (31 loc) · 780 Bytes
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
38
39
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cmath>
using std::cbrt;
using std::pow;
/* TODO: write a brute force test for perfect cubes. Check if
* n = k^3 for some integer k. */
bool isPerfectCube2(const int& n); // Using math libraries
bool isPerfectCube(const int& n); // Bruteforce to test
int main() {
int n;
cout << "Please enter a number: " << endl;
while(cin >> n) {
cout << n << " is ";
isPerfectCube2(n) ? cout << "Perfect Cube!" << endl : cout << "Not a Perfect Cube :(" << endl;
}
return 0;
}
bool isPerfectCube2(const int& n) {
return pow((int)cbrt(n), 3) == n;
}
bool isPerfectCube(const int& n) {
for(int k = 0; k < n; ++k) {
if(k * k * k == n) {
return true;
}
}
return false;
}