-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler29.cpp
59 lines (45 loc) · 1.2 KB
/
euler29.cpp
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
//DESCRIPTION: We start from the naive total and then subtract duplicates. Note that the duplicates are going to occur when one base is an exponent of the other. Since a and b are limited to 100, we only need to check duplicates for "true" bases 2, 3, 5, 6, 7, 10.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int rb[6] = { 2, 3, 5, 6, 7, 10 };
vector<int> te; //contains all exponents of the base so we can track duplicates
int base, be;
int total = 99 * 99; // the naive number of combinations, from which we will subtract duplicates
for (int i = 0; i < 6; i++)
{
te.clear();
base = rb[i];
be = 0; // the max power of the base that we need to check (ie- 6 for base 2 since 2^7 > 100)
int bt = base;
while (bt <= 100)
{
bt *= base;
be++;
}
int duplicate = 0;
for (int j = 0; j < be; j++)
{
for (int b = 2; b <= 100; b++)
{
int oe = (j + 1) * b;
if (find(te.begin(), te.end(), oe) != te.end())
{
duplicate++;
}
else
{
te.push_back(oe);
}
}
}
total -= duplicate;
}
cout << total;
cin.get();
return 0;
}