-
Notifications
You must be signed in to change notification settings - Fork 0
/
63.cpp
64 lines (53 loc) · 1.46 KB
/
63.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
60
61
62
63
64
#include <iostream>
//#define ORIGINAL
// find all numbers where x^digits has digits digits (I'm loving that comment ...)
unsigned int check(unsigned int digits) {
// numbe rof matches
unsigned int count = 0;
// unsigned long long isn't sufficient for the highest digits
// a double has some rounding issues but they don't affect to result
#ifdef ORIGINAL
typedef double Number;
#else
typedef unsigned long long Number;
#endif
// range of valid numbers
// from = 10^(digits-1)
// to = 10^digits - 1
Number to = 1;
for (unsigned int i = 1; i <= digits; i++)
to *= 10;
Number from = to / 10;
to--;
// try all single-digit base numbers
for (unsigned int base = 1; base <= 9; base++) {
// compute power = base ^ digits
Number power = base;
for (unsigned int i = 1; i < digits && power <= to; i++)
power *= base;
// could use C++'s pow(), too
// right number of digits ?
if (power >= from && power <= to) {
count++;
#ifndef ORIGINAL
std::cout << std::fixed << power << std::endl;
#endif
}
}
return count;
}
int main() {
#ifdef ORIGINAL
// check all digits
unsigned int count = 0;
for (unsigned int digits = 1; digits <= 21; digits++) // I observed no results with more than 21 digits
count += check(digits);
std::cout << count << std::endl;
#else
// check only certain digits
unsigned int digits = 9;
std::cin >> digits;
check(digits);
#endif
return 0;
}