-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp25.cpp
71 lines (57 loc) · 1.28 KB
/
p25.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
65
66
67
68
69
70
71
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
typedef std::vector<uint8_t> bigint;
void print(const bigint& v)
{
for (bigint::const_reverse_iterator it = v.rbegin(); it != v.rend(); ++it)
{
int k = *it;
if (k < 0 || k > 9)
{
fprintf(stderr, "Invalid number");
exit(EXIT_FAILURE);
}
printf("%d", k);
}
printf("\n");
}
bigint add(const bigint& a, const bigint& b)
{
bigint c;
int na = a.size();
int nb = b.size();
int carry = 0;
for (int i = 0; i < std::max(na, nb); ++i)
{
int va = (i < na) ? a[i] : 0;
int vb = (i < nb) ? b[i] : 0;
int vc = va + vb + carry;
c.push_back(vc % 10);
carry = vc / 10;
}
if (carry)
c.push_back(1);
return c;
}
int main(int argc, const char* argv[])
{
const int DIGITS = 1000;
bigint a({1});
bigint b({1});
bigint c;
int n = 2;
while(c.size() < DIGITS)
{
n++;
c = add(a, b);
b = a;
a = c;
}
print(c);
printf("N: %d\n", n);
return 0;
}