-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProblemLambda.cpp
604 lines (451 loc) · 11.4 KB
/
ProblemLambda.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include "ProblemLambda.h"
#include "Stopwatch.h"
#include <iostream>
//#include "String.h"
//#include "math.h"
//#include "BigInteger.h"
//#include "Primes.h"
//#include "Stopwatch.h"
//
////#include <iostream>
////#include <fstream>
////#include <map>
////#include <set>
////#include <algorithm>
////#include <iterator>
//
//#include "Problems1-25.h"
//#include "Problems51-75.h"
using namespace std;
//vector<int> ToDigits(int a);
ProblemLambda::ProblemLambda(int number, const char* description, std::function<std::string(void)> solver) :
number(number), description(description), solver(solver)
{
}
void ProblemLambda::Solve()
{
Stopwatch s;
s.Start();
answer = solver();
s.Stop();
durationMs = s.ElapsedMilliseconds();
Report();
}
void ProblemLambda::Report()
{
cout << "Problem " << number << ": " << description << endl;
cout << "Answer: " << answer << " in " << durationMs << "ms" << endl;
cout << endl;
}
/*
// Problem 12
Problem12::Problem12()
{
m_szDescription = "What is the first triangle number to have over five-hundred divisors?";
}
bool Problem12::Solve()
{
//map<int, int> primeFactors = PrimeFactorise(6, 100);
//map<int, int>::iterator mapItr = primeFactors.begin();
//while(mapItr != primeFactors.end())
//{
// cout << (*mapItr).first << "^" << (*mapItr).second << " * ";
// mapItr++;
//}
//cout << endl;
int iTarget = 500;
__int64 iTriangle = 0;
int i = 1;
int numFactors = 0;
map<int, int> primeFactors;
map<int, int>::iterator mapItr;
while(true)
{
iTriangle += i;
primeFactors = PrimeFactorise(iTriangle, 100);
numFactors = 1;
mapItr = primeFactors.begin();
// Calculate number of factors from prime factors
while(mapItr != primeFactors.end())
{
if((*mapItr).second > 0)
numFactors *= (*mapItr).second + 1;
mapItr++;
}
//vFactors = Factorise(iTriangle);
if(numFactors > iTarget)
break;
i++;
}
cout << "The first triangle number to have over " << iTarget << " divisors is " << iTriangle << ", the " << i << "th." << endl;
m_szAnswer = ToString(i);
return true;
}
// Problem 13
Problem13::Problem13()
{
m_szDescription = "Find the first ten digits of the sum of one-hundred 50-digit numbers.";
}
bool Problem13::Solve()
{
ifstream inFile;
inFile.open("../13.txt");
string number;
BigInteger result;
while(!inFile.eof())
{
inFile >> number;
BigInteger n(number);
result.Add(n);
}
inFile.close();
m_szAnswer = result.ToString().substr(0,10);
return true;
}
// Problem 14
Problem14::Problem14()
{
m_szDescription = "Find the longest sequence using a starting number under one million.";
}
int SequenceLength(__int64 n)
{
int count = 1;
while(n != 1)
{
if(n&1) n = 3*n + 1;
else n /= 2;
count++;
}
return count;
}
bool Problem14::Solve()
{
//cout << SequenceLength(13) << endl;
int max = 0;
int count = 0;
int num = 0;
int i = 1;
for(i = 1; i < 1000000; i++)
{
count = SequenceLength((__int64)i);
if(count > max)
{
max = count;
num = i;
}
}
m_szAnswer = ToString(num);
return true;
}
// Problem 15
Problem15::Problem15()
{
m_szDescription = "Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?";
}
// The route problem is a special case of Pascals triangle, where the top of the triangle
// is the bottom right of the grid and contains the possible routes from there to the end
// Therefore all we need to do for an n x n grid is calculate the number at position 2n, n
// of Pascals triangle.
//
// P(r,c) = ((r + 1 - c) / c) * P(r, c - 1)
double PascalsValueAt(double row, double column)
{
if(column == 0) return 1;
return ((row + 1 - column) / column) * PascalsValueAt(row, column - 1);
}
void Test()
{
cout << (PascalsValueAt(0,0) == 1);
cout << (PascalsValueAt(1,0) == 1);
cout << (PascalsValueAt(1,1) == 1);
cout << (PascalsValueAt(2,1) == 2);
cout << (PascalsValueAt(5,3) == 10);
cout << (PascalsValueAt(10,3) == 120);
cout << endl;
cout << (PascalsValueAt(10,3)) << endl;
}
bool Problem15::Solve()
{
m_szAnswer = ToString((__int64)PascalsValueAt(40, 20));
return true;
}
// Problem 16
Problem16::Problem16()
{
m_szDescription = "What is the sum of the digits of the number 2^1000?";
}
bool Problem16::Solve()
{
BigInteger n("2");
for(int i = 1; i < 1000; i++)
{
n.Add(n);
}
vector<int> v = n.GetNumber();
int sum = 0;
for(size_t i = 0; i < v.size(); i++)
sum += v[i];
m_szAnswer = ToString(sum);
return true;
}
// Problem 17
Problem17::Problem17()
{
m_szDescription = "How many letters would be needed to write all the numbers in words from 1 to 1000?";
}
bool Problem17::Solve()
{
int iSum = 0;
string szWords;
for(int i = 1; i <= 1000; i++)
{
szWords = NumToWords(i);
//cout << szWords << " " << (int) szWords.size() << endl;
iSum += (int) szWords.size();
}
m_szAnswer = ToString(iSum);
return true;
}
int BruteForceRoute(vector<vector<int>> triangle, int x, int y, int sum)
{
if(y == triangle.size()) // reached past the bottom
return sum;
if(x == triangle.size() || x == -1) // reached past the side
return sum;
sum += triangle[y][x];
// start more routes
int sumLeft = BruteForceRoute(triangle, x, y+1, sum);
int sumRight = BruteForceRoute(triangle, x+1, y+1, sum);
if(sumLeft > sumRight)
return sumLeft;
return sumRight;
}
int Route(vector<vector<int>> triangle)
{
int a, b, testA, testB, test;
for(size_t y = triangle.size() - 1; y >= 1; y--)
{
for(size_t x = 0; x < triangle[y].size() - 1; x++)
{
a = triangle[y][x];
b = triangle[y][x+1];
test = triangle[y-1][x];
testA = test + a;
testB = test + b;
if(testA > testB)
triangle[y-1][x] = testA;
else
triangle[y-1][x] = testB;
}
}
return triangle[0][0];
}
// Problem 18
Problem18::Problem18()
{
m_szDescription = "Find the maximum sum travelling from the top of the triangle to the base.";
}
bool Problem18::Solve()
{
ifstream inFile;
inFile.open("input\\18.txt");
if(!inFile) return false;
string number;
int iNumber = 0;
vector<vector<int>> numberTriangle;
int lineSize = 1;
while(!inFile.eof())
{
vector<int> line;
for(int i = 0; i < lineSize; i++)
{
inFile >> number;
line.push_back(ToInt(number));
}
numberTriangle.push_back(line);
lineSize++;
}
inFile.close();
m_szAnswer = ToString(Route(numberTriangle));
return true;
}
// Problem 19
Problem19::Problem19()
{
m_szDescription = "How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?";
}
struct Date
{
int dayOfWeek;
int dayOfMonth;
};
bool Problem19::Solve()
{
int currentMonth = 0;
int currentDayOfWeek = 0;
int currentDayOfMonth = 0;
vector<int> months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int currentYear = 1900;
bool leapYear = false;
vector<Date> dates;
int sundaysOnFirstOfMonth = 0;
while (currentYear <= 2000)
{
leapYear = ((currentYear / 4 == 0) && (currentYear / 400 == 0));
if (leapYear)
months[1] = 29;
else
months[1] = 28;
for (currentMonth = 0; currentMonth < 12; currentMonth++)
{
for (currentDayOfMonth = 0; currentDayOfMonth < months[currentMonth]; currentDayOfMonth++)
{
Date date;
date.dayOfWeek = currentDayOfWeek;
date.dayOfMonth = currentDayOfMonth;
dates.push_back(date);
if (currentYear != 1900)
{
if (currentDayOfWeek == 6 && currentDayOfMonth == 0)
sundaysOnFirstOfMonth++;
}
currentDayOfWeek++;
if (currentDayOfWeek > 6)
currentDayOfWeek = 0;
}
}
currentYear++;
}
m_szAnswer = ToString(sundaysOnFirstOfMonth);
return true;
}
// Problem 20
Problem20::Problem20()
{
m_szDescription = "Find the sum of digits in 100!";
}
bool Problem20::Solve()
{
int iSum = 0;
string szWords;
BigInteger n("1");
for(int i = 2; i <= 100; i++)
{
n.Multiply(i);
}
//cout << n.ToString() << endl;
string s = n.ToString();
int sum = 0;
for(size_t i = 0; i < s.size(); i++)
sum += (int)s[i] - 48;
m_szAnswer = ToString(sum);
return true;
}
// Problem 21
Problem21::Problem21()
{
m_szDescription = "Evaluate the sum of all the amicable numbers under 10000.";
}
bool Problem21::Solve()
{
// relates the sum of the proper divisors to a list of numbers with those proper divisors
map<int, vector<int>> sums;
vector<int> sumsByN;
int sum = 0;
for (int i = 0; i < 10000; i++)
{
auto factors = Factorise(i, sum);
sumsByN.push_back(sum);
}
int sumOfAll = 0;
for (int i = 0; i < (int)sumsByN.size(); i++)
{
int sum = sumsByN[i];
//cout << i << ": " << sum << endl;
if (sum >= (int)sumsByN.size()) continue; // can't check
if (i == sumsByN[sum] && i != sum)
{
cout << "Amicable! " << i << " " << sumsByN[i] << endl;
sumOfAll += i + sumsByN[i];
}
}
m_szAnswer = ToString(sumOfAll/2);
return true;
}
Problem22::Problem22()
{
m_szDescription = "What is the total of all the name scores in the file?";
}
int NameScore(const string &name)
{
int sum = 0;
for (char c : name)
{
sum += (c - 64);
}
return sum;
}
bool Problem22::Solve()
{
ifstream inFile;
inFile.open("input/p022_names.txt");
if (!inFile) return false;
set<string> names;
string name;
while (!inFile.eof())
{
inFile >> name;
//cout << "read " << name << endl;
names.insert(name);
}
vector<string> namesVector(names.begin(), names.end());
int64_t score = 0;
int64_t sum = 0;
for (int i = 0; i < (int)namesVector.size(); i++)
{
score = (i+1) * NameScore(namesVector[i]);
if (namesVector[i] == "COLIN")
cout << "COLIN " << (i+1) << " " << score << endl;
sum += score;
}
m_szAnswer = ToString(sum);
return true;
}
Problem23::Problem23()
{
m_szDescription = "Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.";
}
bool Problem23::Solve()
{
return true;
}
// Problem 67
Problem67::Problem67()
{
m_szDescription = "Using an efficient algorithm find the maximal sum in the triangle.";
}
bool Problem67::Solve()
{
ifstream inFile;
inFile.open("input\\67.txt");
if(!inFile) return false;
string number;
int iNumber = 0;
vector<vector<int>> numberTriangle;
int lineSize = 1;
while(!inFile.eof())
{
vector<int> line;
for(int i = 0; i < lineSize; i++)
{
inFile >> number;
line.push_back(ToInt(number));
}
numberTriangle.push_back(line);
lineSize++;
}
inFile.close();
m_szAnswer = ToString(Route(numberTriangle));
return true;
}
*/