-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe016.cpp
More file actions
65 lines (57 loc) · 1.73 KB
/
pe016.cpp
File metadata and controls
65 lines (57 loc) · 1.73 KB
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>
#include <vector>
#include <cmath>
#include <sstream>
#include "pe.h"
template<typename T>
std::string get_reverse_buffer_string(const std::vector<T>& buffer) {
std::stringstream result;
for (int i = buffer.size()-1; i>=0; --i) {
result << buffer[i];
}
return result.str();
}
size_t sum_power_digits(size_t base, size_t exponent) {
size_t result = 0;
//No native data type large enough for 2^1000
//Must use container. Might as well use vector
std::vector<size_t> buffer(1);
buffer[0] = 1;
//max_index stores the position of our most significant digit.
size_t max_index = 0;
size_t digit = 0;
size_t x2 = 0;
size_t sum_next = 0;
for (size_t e = 1; e <= exponent; ++e) {
for (size_t i = 0; i <= max_index; ++i) {
digit = buffer[i];
x2 = digit * base;
sum_next /=10;
buffer[i] = x2 % 10 + sum_next %10;
sum_next += x2;
if (max_index < i + (sum_next ==0 ? 0 : (size_t)(std::log10(sum_next)))) {
size_t s = (size_t)(i + std::log10(sum_next));
max_index = std::max(max_index, s);
buffer.resize(s+1);
}
}
//std::cout << "2^" << e << ": " << get_reverse_buffer_string(buffer) << std::endl;;
}
for (size_t i = 0; i <= max_index; ++i) {
result += buffer[i];
}
return result;
}
class pe016 : public pe_base {
void run_test() {
int ans = 1366;
int value = sum_power_digits(2, 1000);
check("016", ans, value);
}
};
int main(int argc, char** argv) {
pe016 test;
test.go();
std::cout << test.get_message() << std::endl;
return test.exit_code();
}