-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.c
More file actions
executable file
·4661 lines (4148 loc) · 129 KB
/
euler.c
File metadata and controls
executable file
·4661 lines (4148 loc) · 129 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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if !defined(COMPILER_MSVC)
#define COMPILER_MSVC 0
#endif
#if !defined(COMPILER_GCC)
#define COMPILER_GCC 0
#endif
#if !defined(COMPILER_LLVM)
#define COMPILER_LLVM 0
#endif
#if !COMPILER_MSVC && !COMPILER_GCC && !COMPILER_LLVM
#if _MSC_VER
#undef COMPILER_MSVC
#define COMPILER_MSVC 1
#endif
#endif
#include <stdint.h>
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int b32;
typedef float f32;
typedef double f64;
#define internal static
#define Kilobytes(Value) ((Value) * 1000LL)
#define Megabytes(Value) (Kilobytes(Value) * 1000LL)
#define Gigabytes(Value) (Megabytes(Value) * 1000LL)
#define Terabytes(Value) (Gigabytes(Value) * 1000LL)
#if DEBUG
#define Assert(Expression) if (!(Expression)) {*(int *)0 = 0;}
#define InvalidCodePath {*(int *)0 = 0;}
#define InvalidDefaultCase default: {InvalidCodePath;}
#else
#define Assert(Expression)
#define InvalidCodePath
#define InvalidDefaultCase
#endif
#define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
#define false 0
#define true 1
internal void
ZeroBytes(char *Buffer, u32 BytesCount)
{
for (u32 Byte = 0; Byte < BytesCount; Byte++)
{
Buffer[Byte] = 0;
}
}
typedef struct
{
u32 Size;
u8 *Base;
u32 Used;
s32 TempCount;
} memory_arena;
internal void
InitializeArena(memory_arena *Arena, u32 Size, void *Base)
{
Arena->Size = Size;
Arena->Base = (u8 *)Base;
Arena->Used = 0;
Arena->TempCount = 0;
}
#define PushStruct(Arena, type) (type *)PushSize(Arena, sizeof(type))
#define PushArray(Arena, Count, type) (type *)PushSize(Arena, (Count) * sizeof(type))
#define PushZeroStruct(Arena, type) (type *)PushZeroSize(Arena, sizeof(type))
#define PushZeroArray(Arena, Count, type) (type *)PushZeroSize(Arena, (Count) * sizeof(type))
internal void *
PushSize(memory_arena *Arena, u32 Size)
{
Assert((Arena->Used + Size) <= Arena->Size);
void *Result = Arena->Base + Arena->Used;
Arena->Used += Size;
return Result;
}
internal void *
PushZeroSize(memory_arena *Arena, u32 Size)
{
Assert((Arena->Used + Size) <= Arena->Size);
void *Result = Arena->Base + Arena->Used;
u8 *P = Result;
Arena->Used += Size;
while (P < Arena->Base + Arena->Used)
*P++ = 0;
return Result;
}
typedef struct
{
memory_arena *Arena;
u32 Used;
} temporary_memory;
internal temporary_memory
BeginTemporaryMemory(memory_arena *Arena)
{
temporary_memory Result;
Result.Arena = Arena;
Result.Used = Arena->Used;
++Arena->TempCount;
return Result;
}
internal void
EndTemporaryMemory(temporary_memory TempMemory)
{
memory_arena *Arena = TempMemory.Arena;
Assert(Arena->Used >= TempMemory.Used);
Arena->Used = TempMemory.Used;
--Arena->TempCount;
Assert(Arena->TempCount >= 0);
}
internal void
CheckArena(memory_arena *Arena)
{
Assert(Arena->TempCount == 0);
}
internal void
SubArena(memory_arena *Result, memory_arena *Arena, u32 Size)
{
Result->Size = Size;
Result->Base = (u8 *)PushSize(Arena, Size);
Result->Used = 0;
Result->TempCount = 0;
}
internal b32
NumberOfDigitsU32(u32 Number)
{
// NOTE(vincent): Unused atm
b32 Result = false;
u32 NumberOfDigits = 1;
Assert(Number < 1000 * 1000 * 1000);
u32 Tens = 10;
while (Tens < Number)
{
Tens *= 10;
NumberOfDigits++;
}
return Result;
}
internal b32
IsPalindromeU32(u32 Number)
{
b32 Result = false;
if (Number < 1000000)
{
Number = (Number % 100001) % 10010;
Result = (Number % 1100 == 0);
}
else if (Number > 10000)
{
Number = (Number % 10001) % 1010;
Result = (Number % 100) == 0;
}
else
InvalidCodePath;
return Result;
}
#include <stdio.h>
internal void
Problem001()
{
// problem 1: sum of all positive multiples of 3 or 5 below 1000.
// This could be done on pen and paper...
u32 Sum = 0;
for (int k = 1; k < 1000; k++)
{
if (k % 3 == 0 || k % 5 == 0)
Sum += k;
}
printf("%d\n", Sum);
}
internal void
Problem002()
{
// problem 2: Sum of even-valued Fibonacci numbers below 4 million.
u32 Fib0 = 0;
u32 Fib1 = 1;
u32 Result002 = 0;
while (Fib1 < 4000 * 1000)
{
if (Fib1 & 1)
Result002 += Fib1;
u32 Temp = Fib0 + Fib1;
Fib0 = Fib1;
Fib1 = Temp;
}
printf("%d\n", Result002);
}
internal void
Problem003()
{
// problem 3: largest prime factor of 600851475143
u64 BigNumber = 600851475143;
u64 Divisor = 3;
for (; BigNumber > 1; Divisor+=2)
{
while (BigNumber % Divisor == 0)
BigNumber /= Divisor;
}
Divisor -= 2;
printf("%lld\n", Divisor);
}
internal void
Problem004()
{
// problem 4: Largest palindrome made from the product of two 3-digit numbers.
u32 BigFactor = 999;
u32 SmallFactor = 999;
u32 BiggestPalindrome = 0;
for (;;)
{
u32 Product = BigFactor * SmallFactor;
if (Product < BiggestPalindrome || SmallFactor <= 100)
{
if (BigFactor == 100 || BigFactor == SmallFactor)
break;
BigFactor--;
SmallFactor = BigFactor;
}
else if (IsPalindromeU32(Product))
{
BiggestPalindrome = Product;
BigFactor--;
SmallFactor = BigFactor;
}
else
{
SmallFactor--;
}
}
printf("%d\n", BiggestPalindrome);
}
internal void
Problem005()
{
// problem 5: lcm(1, 2, ..., 20)
u64 WorkingFactor = 2;
u64 LastLCM = 1;
u64 Result005 = 1;
for (;;)
{
Result005 += LastLCM;
while (Result005 % WorkingFactor == 0)
{
WorkingFactor++;
if (WorkingFactor > 20)
goto LABEL_DONE_005;
LastLCM = Result005;
}
}
LABEL_DONE_005:
printf("%lld\n", Result005);
}
internal void
Problem006()
{
// problem 6: difference between sum of squares and square of sum of 1,2,...,100
u32 SquareOfSum = 50*101*50*101;
u32 SumOfSquares = (100*101*201)/6;
u32 Difference = SquareOfSum - SumOfSquares;
printf("%d\n", Difference);
}
internal b32
IsPrime(u32 N)
{
Assert(N > 5);
b32 Result = true;
u32 Mod6 = N % 6;
if (Mod6 == 1 || Mod6 == 5)
{
for (u32 Divisor = 5; Divisor*Divisor <= N; Divisor+=2)
{
if (N % Divisor == 0)
{
Result = false;
break;
}
}
}
else
Result = false;
return Result;
}
internal void
Problem007()
{
// problem 7: 10001st prime number
u32 NextCandidate = 5;
u32 PrimesCount = 3;
while (PrimesCount < 10001)
{
NextCandidate += 2;
while(!IsPrime(NextCandidate))
{
NextCandidate += 2;
}
PrimesCount++;
}
printf("%d\n", NextCandidate);
}
internal void
Problem008()
{
// Find the largest product given by 13 adjacent digits in the following 1000-digit number.
char *Number =
"73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
"53697817977846174064955149290862569321978468622482"
"83972241375657056057490261407972968652414535100474"
"82166370484403199890008895243450658541227588666881"
"16427171479924442928230863465674813919123162824586"
"17866458359124566529476545682848912883142607690042"
"24219022671055626321111109370544217506941658960408"
"07198403850962455444362981230987879927244284909188"
"84580156166097919133875499200524063689912560717606"
"05886116467109405077541002256983155200055935729725"
"71636269561882670428252483600823257530420752963450";
u32 FirstDigitOffset = 0;
u32 LastDigitOffset = 0;
u64 LargestProduct = 1;
u64 CurrentProduct = 1;
while (LastDigitOffset < 1000)
{
switch (Number[LastDigitOffset])
{
case '0':
FirstDigitOffset = LastDigitOffset + 1;
CurrentProduct = 1;
break;
default:
{
u32 ProductDigitCount = LastDigitOffset + 1 - FirstDigitOffset;
if (ProductDigitCount > 13)
{
CurrentProduct /= (Number[FirstDigitOffset] - '0');
FirstDigitOffset++;
}
CurrentProduct *= Number[LastDigitOffset] - '0';
if (CurrentProduct > LargestProduct)
LargestProduct = CurrentProduct;
}
}
LastDigitOffset++;
}
printf("%lld\n", LargestProduct);
}
internal void
Problem009()
{
// product abc where (a,b,c) is the Pythagorean triplet such that a + b + c = 1000.
u32 Product = 1;
for (u32 a = 1; a < 334; a++)
{
// NOTE(vincent): right isocele rectangle cannot have integral sides
for (u32 b = a + 1; 2*b < 1000 - a; b++)
{
u32 c = 1000 - b - a;
if (c*c == b*b + a*a)
{
Product = a*b*c;
goto Label_Done009;
}
}
}
Label_Done009:
printf("%d\n", Product);
}
internal void
Problem010()
{
// sum of all the primes below two million.
u64 Sum = 2 + 3 + 5;
u32 NextCandidate = 7;
while (NextCandidate < 2000 * 1000)
{
if (IsPrime(NextCandidate))
{
Sum += NextCandidate;
}
NextCandidate += 2;
}
printf("%lld\n", Sum);
}
typedef struct
{
char *Memory;
size_t Size;
b32 Success;
} push_read_file;
internal push_read_file
PushReadFile(memory_arena *Arena, char *Filename)
{
push_read_file Result = {0};
FILE *File = fopen(Filename, "rb");
if (File)
{
fseek(File, 0, SEEK_END);
Result.Size = ftell(File);
fseek(File, 0, SEEK_SET);
u32 AvailableSize = Arena->Size - Arena->Used;
if (Result.Size <= AvailableSize)
{
Result.Memory = PushArray(Arena, (u32)Result.Size, char);
size_t BytesWritten = fread(Result.Memory, 1, Result.Size, File);
if (BytesWritten == Result.Size)
Result.Success = true;
}
fclose(File);
}
return Result;
}
internal void
Problem011(memory_arena *Arena)
{
// Read a 20*20 grid of numbers and find the largest product of four adjacent digits
// in one direction (horizontal or vertical or diagonal).
temporary_memory TempMemory = BeginTemporaryMemory(Arena);
push_read_file ReadFile = PushReadFile(Arena, "text011");
Assert(ReadFile.Success && ReadFile.Memory && ReadFile.Size > 0);
u32 *Grid = PushArray(Arena, 20*20, u32);
u32 WriteIndex = 0;
b32 InNumber = true;
char *At = (char *)ReadFile.Memory;
char *End = At + ReadFile.Size;
for (; At < End; ++At)
{
if ('0' <= *At && *At <= '9')
{
InNumber = true;
Grid[WriteIndex] = Grid[WriteIndex] * 10 + (*At - '0');
}
else
{
if (InNumber)
{
InNumber = false;
WriteIndex++;
}
}
}
Assert(WriteIndex == 400 || WriteIndex == 399);
u32 LargestProduct = 1;
for (u32 X = 0; X <= 396; X++)
{
if (X % 20 < 17)
{
u32 HorizontalProduct = Grid[X] * Grid[X+1] * Grid[X+2] * Grid[X+3];
LargestProduct = HorizontalProduct > LargestProduct ? HorizontalProduct : LargestProduct;
}
if (X < 340)
{
u32 VerticalProduct = Grid[X] * Grid[X+20] * Grid[X+40] * Grid[X+60];
LargestProduct = VerticalProduct > LargestProduct ? VerticalProduct : LargestProduct;
}
if (X % 20 < 17 && X < 340)
{
u32 DescendingDiagonalProduct = Grid[X] * Grid[X+21] * Grid[X+42] * Grid[X+63];
LargestProduct = DescendingDiagonalProduct > LargestProduct ?
DescendingDiagonalProduct : LargestProduct;
}
if (X % 20 > 2 && X < 340)
{
u32 AscendingDiagonalProduct = Grid[X] * Grid[X+19] * Grid[X+38] * Grid[X+57];
LargestProduct = AscendingDiagonalProduct > LargestProduct ?
AscendingDiagonalProduct : LargestProduct;
}
}
printf("%d\n", LargestProduct);
EndTemporaryMemory(TempMemory);
}
internal void
Problem012()
{
// smallest triangle number that has over 500 divisors (between 1 and itself, inclusive).
u32 Triangle = 1;
u32 NextIncrement = 2;
for (;;)
{
Triangle += NextIncrement;
NextIncrement++;
u32 DivisorCount = 1;
u32 DivisorCandidate = 2;
while (DivisorCandidate * DivisorCandidate < Triangle)
{
if (Triangle % DivisorCandidate == 0)
{
DivisorCount += 2;
}
DivisorCandidate++;
}
if (DivisorCandidate * DivisorCandidate == Triangle)
DivisorCount++;
if (DivisorCount > 500)
break;
}
printf("%d\n", Triangle);
}
internal void
Problem013(memory_arena *Arena)
{
// Text file has a hundred 50-digit numbers, one per line. Work out the first ten digits of the sum.
temporary_memory TempMemory = BeginTemporaryMemory(Arena);
push_read_file ReadFile = PushReadFile(Arena, "text013");
Assert(ReadFile.Success && ReadFile.Memory && ReadFile.Size > 0);
char *At = (char *)ReadFile.Memory;
char *End = At + ReadFile.Size;
u64 *DigitSums = PushArray(Arena, 50, u64);
s32 WriteIndex = 49;
for (; At < End; ++At)
{
if ('0' <= *At && *At <= '9')
{
DigitSums[WriteIndex] += *At - '0';
WriteIndex = WriteIndex == 0 ? 49 : WriteIndex-1;
}
}
for (u32 Index = 0; Index < 49; Index++)
{
u64 Carry = DigitSums[Index] / 10;
DigitSums[Index + 1] += Carry;
}
u64 Result = DigitSums[49];
u32 JoinIndex = 48;
while (Result < 1000LLU * 1000 * 1000)
{
Result = Result * 10 + (DigitSums[JoinIndex] % 10);
JoinIndex--;
}
while (Result >= 1000LLU * 1000 * 1000 * 10)
{
Result /= 10;
}
EndTemporaryMemory(TempMemory);
printf("%lld\n", Result);
}
internal void
Problem014()
{
// find the starting number < 1 million that produces the longest Collatz chain.
u32 BestStartingNumber = 0;
u32 BestLength = 0;
u32 CurrentStartingNumber = 1;
while (CurrentStartingNumber < 1000 * 1000)
{
u32 CurrentLength = 0;
for (u64 N = CurrentStartingNumber;
N != 1;
N = (N & 1) ? 3*N + 1 : N >> 1)
{
CurrentLength++;
}
if (CurrentLength > BestLength)
{
BestLength = CurrentLength;
BestStartingNumber = CurrentStartingNumber;
}
CurrentStartingNumber++;
// TODO(vincent): could we skip some starting candidates ?
}
printf("%d\n", BestStartingNumber);
}
internal void
Problem015()
{
// 40 choose 20. aka 40! / (20! 20!)
// https://en.wikipedia.org/wiki/Central_binomial_coefficient
u64 CentralBinomialCoef = 2;
for (u32 N = 1; N < 20; N++)
{
CentralBinomialCoef *= 4*N + 2;
CentralBinomialCoef /= N+1;
}
printf("%lld\n", CentralBinomialCoef);
}
internal void
Problem016()
{
// sum of digits of 2^1000.
u16 Digits[500] = {0};
Digits[0] = 1;
for (u32 Power = 0; Power < 1000; Power++)
{
for (u32 Index = 0; Index < ArrayCount(Digits); Index++)
{
Digits[Index] *= 2;
}
for (u32 Index = 0; Index < ArrayCount(Digits)-1; Index++)
{
u16 Carry = Digits[Index] / 10;
Digits[Index+1] += Carry;
Digits[Index] %= 10;
}
}
u32 DigitSum = 0;
for (u32 Index = 0; Index < ArrayCount(Digits); Index++)
DigitSum += Digits[Index];
printf("%d\n", DigitSum);
}
internal void
Problem017()
{
// count the letters used in: one two three four five... one thousand.
// Don't count spaces or hyphens.
u8 C[20] = {0};
C[0] = 0;
C[1] = 3; // one
C[2] = 3; // two
C[3] = 5; // three
C[4] = 4; // four
C[5] = 4; // five
C[6] = 3; // six
C[7] = 5; // seven
C[8] = 5; // eight
C[9] = 4; // nine
C[10] = 3; // ten
C[11] = 6; // eleven
C[12] = 6; // twelve
C[13] = 8; // thirteen
C[14] = 8; // fourteen
C[15] = 7; // fifteen
C[16] = 7; // sixteen
C[17] = 9; // seventeen
C[18] = 8; // eighteen
C[19] = 8; // nineteen
u8 TenCount[10];
TenCount[0] = 0;
TenCount[1] = 0;
TenCount[2] = 6; // twenty
TenCount[3] = 6; // thirty
TenCount[4] = 5; // forty (curse you, English!)
TenCount[5] = 5; // fifty
TenCount[6] = 5; // sixty
TenCount[7] = 7; // seventy
TenCount[8] = 6; // eighty
TenCount[9] = 6; // ninety
u32 LetterCount = 0;
LetterCount += 3 + 8; // "one thousand"
for (u32 Number = 1; Number < 1000; Number++)
{
u32 HundredUnit = Number / 100;
u32 HundredRemainder = Number % 100;
u32 TenUnit = (Number / 10) % 10;
u32 TenRemainder = Number % 10;
if (HundredUnit > 0)
{
LetterCount += C[HundredUnit] + 7;
if (HundredRemainder > 0)
LetterCount += 3; // "and"
}
if (TenUnit >= 2)
LetterCount += TenCount[TenUnit] + C[TenRemainder];
else
LetterCount += C[HundredRemainder];
}
printf("%d\n", LetterCount);
}
internal void
Problem018()
{
// max sum path. https://projecteuler.net/problem=18
u32 Pyramid[] = {
75,
95, 64,
17, 47, 82,
18, 35, 87, 10,
20, 4, 82, 47, 65,
19, 1, 23, 75, 3, 34,
88, 2, 77, 73, 7, 63, 67,
99, 65, 4, 28, 6, 16, 70, 92,
41, 41, 26, 56, 83, 40, 80, 70, 33,
41, 48, 72, 33, 47, 32, 37, 16, 94, 29,
53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14,
70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57,
91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48,
63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31,
4 , 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23
};
u32 TriangleNumbers[15];
for (u32 Index = 0; Index < ArrayCount(TriangleNumbers); Index++)
TriangleNumbers[Index] = (Index*(Index+1))/2;
/*
0
1 2
3 4 5
6 7 8 9
*/
for (s32 RowIndex = 13; RowIndex >= 0; RowIndex--)
{
for (s32 ColIndex = 0; ColIndex <= RowIndex; ColIndex++)
{
u32 Index = TriangleNumbers[RowIndex] + ColIndex;
u32 IndexLeft = TriangleNumbers[RowIndex+1] + ColIndex;
u32 IndexRight = TriangleNumbers[RowIndex+1] + ColIndex + 1;
u32 Left = Pyramid[IndexLeft];
u32 Right = Pyramid[IndexRight];
u32 Max = Left > Right ? Left : Right;
Pyramid[Index] += Max;
}
}
printf("%d\n", Pyramid[0]);
}
typedef struct
{
u32 Weekday; // 1 to 7
u32 Month; // 1 to 12
u32 MonthDay; // 1 to 31
u32 Year;
} date;
internal void
NextDay(date *Date)
{
Date->Weekday = Date->Weekday == 7 ? 1 : Date->Weekday + 1;
Date->MonthDay++;
switch (Date->Month)
{
case 4:
case 6:
case 9:
case 11:
{
if (Date->MonthDay == 31)
{
Date->MonthDay = 1;
Date->Month++;
}
} break;
case 2:
{
b32 LeapYear = ( ((Date->Year % 4) == 0 && (Date->Year % 100 != 0)) ||
Date->Year % 400 == 0 );
if (Date->MonthDay == 30 || (Date->MonthDay == 29 && !LeapYear))
{
Date->MonthDay = 1;
Date->Month = 3;
}
} break;
default:
{
if (Date->MonthDay == 32)
{
Date->Month = Date->Month == 12 ? 1 : Date->Month+1;
if (Date->Month == 1)
Date->Year++;
Date->MonthDay = 1;
}
}
}
}
internal void
Problem019()
{
// How many Sundays fell on the first of the month during the twentieth century
// (1 Jan 1901 to 31 Dec 2000)?
/*
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century
unless it is divisible by 400.
*/
date Date;
Date.Weekday = 1;
Date.Month = 1;
Date.MonthDay = 1;
Date.Year = 1900;
u32 SundayOnFirstCount = 0;
while (Date.Year <= 2000)
{
if (Date.Year >= 1901 && Date.MonthDay == 1 && Date.Weekday == 7)
SundayOnFirstCount++;
NextDay(&Date);
}
printf("%d\n", SundayOnFirstCount);
}
internal void
Problem020()
{
// Sum of digits of 100!
// 100! has less than 200 digits.
// a u32 should be able to hold 6 digits while being able to avoid overflow when multiplying by 100.
// We could store the "result" of 100! in thirty-three u32s or less.
u32 Product[33] = {0};
Product[0] = 1;
for (u32 Factor = 1; Factor < 100; Factor++)
{
for (u32 Index = 0; Index < ArrayCount(Product); Index++)
Product[Index] *= Factor;
u32 Carry = 0;
for (u32 Index = 0; Index < ArrayCount(Product); Index++)
{
Carry = Product[Index] / (1000 * 1000);
if (Carry > 0)
{
Assert(Index < ArrayCount(Product) - 1);
Product[Index] %= 1000 * 1000;
Product[Index+1] += Carry;
}
}
}
u32 SumDigits = 0;
for (u32 Index = 0; Index < ArrayCount(Product); Index++)
{
for (u32 N = Product[Index]; N > 0; N /= 10)
SumDigits += N % 10;
}
printf("%d\n", SumDigits);
}
internal u32
SumProperDivisors(u32 N)
{
u32 Result = 0;
if (N > 1)
{
Result = 1;
for (u32 DivisorCandidate = 2; DivisorCandidate*DivisorCandidate <= N; DivisorCandidate++)
{
if (N % DivisorCandidate == 0)
{
Result += DivisorCandidate;
if (DivisorCandidate * DivisorCandidate != N)
{
u32 BroDivisor = N / DivisorCandidate;
Result += BroDivisor;
}
}
}
}
return Result;
}
internal void
Problem021()
{
// Sum of all amicable numbers < 10000.
// d(n): sum of proper divisors (divisors between 1 inclusive and n exclusive).
// Distinct naturals a and b form an amicable pair when d(a) = b and d(b) = a.
u32 SumAmicables = 0;
for (u32 N = 2; N < 10000; N++)
{
u32 SumPD = SumProperDivisors(N);
if (SumPD != N)
{
u32 OtherSum = SumProperDivisors(SumPD);
if (OtherSum == N)
SumAmicables += N;
}
}
printf("%d\n", SumAmicables);
}
typedef struct
{
char *Base;
u32 Size;
} string;
internal b32
StringLEQ(string A, string B)
{
b32 Result = A.Size <= B.Size;
u32 MinSize = A.Size < B.Size ? A.Size : B.Size;
for (u32 Index = 0; Index < MinSize; Index++)
{
if (A.Base[Index] > B.Base[Index])
{
Result = false;
break;
}
if (A.Base[Index] < B.Base[Index])
{
Result = true;
break;
}
}
return Result;
}
internal string
StringFromLiteral(char *Literal)
{
string Result;
Result.Base = Literal;
Result.Size = 0;
while (*Literal)
{
Literal++;
Result.Size++;