-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler20.cpp
68 lines (54 loc) · 1.42 KB
/
euler20.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
//DESCRIPTION: 100! has fewer digits than 200 digits (which is 100^100), so we keep track of each digit in a 200-int array. Then, we simply implement the hand multiplication algorithm. Note that we actually compute 99! so that the multiplier can always be assumed to have two digits, since the final factor of 100 won't change the sum of digits.
#include <iostream>
using namespace std;
int sdm(int* output, int a, int b, int carry) // computes a*b + carry, places the unit value in output and returns the carry
{
int result = a*b + carry;
*output = result % 10;
return result / 10;
}
int main()
{
int digits[200];
int pp1[200];
int pp2[200];
for (int i = 0; i < 200; i++)
{
digits[i] = 0;
pp1[i] = 0;
pp2[i] = 0;
}
digits[0] = 1;
int d1, d2, carry1, carry2;
for (int i = 2; i <= 99; i++)
{
d1 = i % 10;
d2 = i / 10;
//compute the partial products
carry1 = 0;
carry2 = 0;
for (int j = 0; j < 200; j++)
{
carry1 = sdm(&pp1[j], digits[j], d1, carry1);
carry2 = sdm(&pp2[j + 1], digits[j], d2, carry2);
}
//sum the partial products
carry1 = 0;
for (int j = 0; j < 200; j++)
{
digits[j] = pp1[j] + pp2[j] + carry1;
carry1 = 0;
if (digits[j] > 9)
{
digits[j] = digits[j] % 10;
carry1 = 1;
}
}
}
int sum = 0;
for (int j = 0; j < 200; j++)
sum += digits[j];
cout << sum;
cin.get();
return 0;
}