-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler56.cpp
85 lines (73 loc) · 1.48 KB
/
euler56.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//DESCRIPTION: Uses the hand multiplication algorithm developed for problem 20 to iterate through all combinations.
#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] = 9;
digits[1] = 9;
int d1, d2, carry1, carry2, sum;
int max = 0;
for (int a = 2; a <= 99; a++)
{
for (int i = 0; i < 200; i++)
{
digits[i] = 0;
pp1[i] = 0;
pp2[i] = 0;
}
digits[0] = a % 10;
digits[1] = a / 10;
d1 = a % 10;
d2 = a / 10;
for (int b = 2; b <= 99; b++)
{
//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;
}
}
sum = 0;
for (int j = 0; j < 200; j++)
{
sum += digits[j];
}
if (sum > max)
{
max = sum;
}
}
}
cout << max;
cin.get();
return 0;
}