-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_cubes.cc
68 lines (55 loc) · 1.28 KB
/
two_cubes.cc
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Compile with:
* g++ -o two_cubes -Wall -Wextra -std=c++11 two_cubes.cc
*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
typedef unsigned int uint32;
typedef unsigned long long int uint64;
static std::map<uint32, uint32>
decompose_primes(uint64 num)
{
std::map<uint32, uint32> res;
uint32 divider = 2;
while (num != 1) {
uint32 count = 0;
while (num % divider == 0) {
count += 1;
num /= divider;
}
if (count > 0)
res[divider] = count;
divider += 1;
}
return res;
}
int
main(int argc, char **argv)
{
uint64 number;
std::map<uint32, uint32> primes;
if (argc != 2) {
std::cerr
<< "Determine if a number is a product of two cubes." << std::endl
<< "usage: " << argv[0] << " NUMBER" << std::endl;
return 1;
}
std::istringstream(argv[1]) >> number;
primes = decompose_primes(number);
std::cout << "1";
for (const auto& e : primes) {
std::cout << " x " << e.first << "^" << e.second;
}
std::cout
<< ": "
<< (std::all_of(primes.begin(), primes.end(),
[](const std::pair<uint32, uint32>& e){
return e.second % 3 == 0;
}) && primes.size() >= 2
? "yes" : "no")
<< "."
<< std::endl;
return 0;
}