Fortran Implementation of Problem 13
View source code here on GitHub!
- integer Problem0013/p0013()
1! Project Euler Problem 13
2!
3! Problem:
4!
5! Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
6! 37107287533902102798797998220837590246510135740250
7! 46376937677490009712648124896970078050417018260538
8! 74324986199524741059474233309513058123726617309629
9! 91942213363574161572522430563301811072406154908250
10! 23067588207539346171171980310421047513778063246676
11! 89261670696623633820136378418383684178734361726757
12! 28112879812849979408065481931592621691275889832738
13! 44274228917432520321923589422876796487670272189318
14! 47451445736001306439091167216856844588711603153276
15! 70386486105843025439939619828917593665686757934951
16! 62176457141856560629502157223196586755079324193331
17! 64906352462741904929101432445813822663347944758178
18! 92575867718337217661963751590579239728245598838407
19! 58203565325359399008402633568948830189458628227828
20! 80181199384826282014278194139940567587151170094390
21! 35398664372827112653829987240784473053190104293586
22! 86515506006295864861532075273371959191420517255829
23! 71693888707715466499115593487603532921714970056938
24! 54370070576826684624621495650076471787294438377604
25! 53282654108756828443191190634694037855217779295145
26! 36123272525000296071075082563815656710885258350721
27! 45876576172410976447339110607218265236877223636045
28! 17423706905851860660448207621209813287860733969412
29! 81142660418086830619328460811191061556940512689692
30! 51934325451728388641918047049293215058642563049483
31! 62467221648435076201727918039944693004732956340691
32! 15732444386908125794514089057706229429197107928209
33! 55037687525678773091862540744969844508330393682126
34! 18336384825330154686196124348767681297534375946515
35! 80386287592878490201521685554828717201219257766954
36! 78182833757993103614740356856449095527097864797581
37! 16726320100436897842553539920931837441497806860984
38! 48403098129077791799088218795327364475675590848030
39! 87086987551392711854517078544161852424320693150332
40! 59959406895756536782107074926966537676326235447210
41! 69793950679652694742597709739166693763042633987085
42! 41052684708299085211399427365734116182760315001271
43! 65378607361501080857009149939512557028198746004375
44! 35829035317434717326932123578154982629742552737307
45! 94953759765105305946966067683156574377167401875275
46! 88902802571733229619176668713819931811048770190271
47! 25267680276078003013678680992525463401061632866526
48! 36270218540497705585629946580636237993140746255962
49! 24074486908231174977792365466257246923322810917141
50! 91430288197103288597806669760892938638285025333403
51! 34413065578016127815921815005561868836468420090470
52! 23053081172816430487623791969842487255036638784583
53! 11487696932154902810424020138335124462181441773470
54! 63783299490636259666498587618221225225512486764533
55! 67720186971698544312419572409913959008952310058822
56! 95548255300263520781532296796249481641953868218774
57! 76085327132285723110424803456124867697064507995236
58! 37774242535411291684276865538926205024910326572967
59! 23701913275725675285653248258265463092207058596522
60! 29798860272258331913126375147341994889534765745501
61! 18495701454879288984856827726077713721403798879715
62! 38298203783031473527721580348144513491373226651381
63! 34829543829199918180278916522431027392251122869539
64! 40957953066405232632538044100059654939159879593635
65! 29746152185502371307642255121183693803580388584903
66! 41698116222072977186158236678424689157993532961922
67! 62467957194401269043877107275048102390895523597457
68! 23189706772547915061505504953922979530901129967519
69! 86188088225875314529584099251203829009407770775672
70! 11306739708304724483816533873502340845647058077308
71! 82959174767140363198008187129011875491310547126581
72! 97623331044818386269515456334926366572897563400500
73! 42846280183517070527831839425882145521227251250327
74! 55121603546981200581762165212827652751691296897789
75! 32238195734329339946437501907836945765883352399886
76! 75506164965184775180738168837861091527357929701337
77! 62177842752192623401942399639168044983993173312731
78! 32924185707147349566916674687634660915035914677504
79! 99518671430235219628894890102423325116913619626622
80! 73267460800591547471830798392868535206946944540724
81! 76841822524674417161514036427982273348055556214818
82! 97142617910342598647204516893989422179826088076852
83! 87783646182799346313767754307809363333018982642090
84! 10848802521674670883215120185883543223812876952786
85! 71329612474782464538636993009049310363619763878039
86! 62184073572399794223406235393808339651327408011116
87! 66627891981488087797941876876144230030984490851411
88! 60661826293682836764744779239180335110989069790714
89! 85786944089552990653640447425576083659976645795096
90! 66024396409905389607120198219976047599490197230297
91! 64913982680032973156037120041377903785566085089252
92! 16730939319872750275468906903707539413042652315011
93! 94809377245048795150954100921645863754710598436791
94! 78639167021187492431995700641917969777599028300699
95! 15368713711936614952811305876380278410754449733078
96! 40789923115535562561142322423255033685442488917353
97! 44889911501440648020369068063960672322193204149535
98! 41503128880339536053299340368006977710650566631954
99! 81234880673210146739058568557934581403627822703280
100! 82616570773948327592232845941706525094512325230608
101! 22918802058777319719839450180888072429661980811197
102! 77158542502016545090413245809786882778948721859617
103! 72107838435069186155435662884062257473692284509516
104! 20849603980134001723930671666823555245252804609722
105! 53503534226472524250874054075591789781264330331690
106
107module Problem0013
108 use constants
109 implicit none
110contains
111 integer(i18t) function p0013() result(answer)
112 integer(i18t), dimension(3, 100) :: numbers
113 integer(i18t), dimension(3) :: arr = (/ 0, 0, 0 /)
114 integer(i18t) :: ten18 = 1000000000000000000_i18t
115 integer(i18t) :: ten10 = 10000000000_i18t
116 integer :: i, j
117
118 ! Manually initialize the grid
119 data numbers / &
120 37107287533902_i18t, 102798797998220837_i18t, 590246510135740250_i18t, 46376937677490_i18t, 9712648124896970_i18t, 78050417018260538_i18t, 74324986199524_i18t, 741059474233309513_i18t, 58123726617309629_i18t, 91942213363574_i18t, 161572522430563301_i18t, 811072406154908250_i18t, &
121 23067588207539_i18t, 346171171980310421_i18t, 47513778063246676_i18t, 89261670696623_i18t, 633820136378418383_i18t, 684178734361726757_i18t, 28112879812849_i18t, 979408065481931592_i18t, 621691275889832738_i18t, 44274228917432_i18t, 520321923589422876_i18t, 796487670272189318_i18t, &
122 47451445736001_i18t, 306439091167216856_i18t, 844588711603153276_i18t, 70386486105843_i18t, 25439939619828917_i18t, 593665686757934951_i18t, 62176457141856_i18t, 560629502157223196_i18t, 586755079324193331_i18t, 64906352462741_i18t, 904929101432445813_i18t, 822663347944758178_i18t, &
123 92575867718337_i18t, 217661963751590579_i18t, 239728245598838407_i18t, 58203565325359_i18t, 399008402633568948_i18t, 830189458628227828_i18t, 80181199384826_i18t, 282014278194139940_i18t, 567587151170094390_i18t, 35398664372827_i18t, 112653829987240784_i18t, 473053190104293586_i18t, &
124 86515506006295_i18t, 864861532075273371_i18t, 959191420517255829_i18t, 71693888707715_i18t, 466499115593487603_i18t, 532921714970056938_i18t, 54370070576826_i18t, 684624621495650076_i18t, 471787294438377604_i18t, 53282654108756_i18t, 828443191190634694_i18t, 37855217779295145_i18t, &
125 36123272525000_i18t, 296071075082563815_i18t, 656710885258350721_i18t, 45876576172410_i18t, 976447339110607218_i18t, 265236877223636045_i18t, 17423706905851_i18t, 860660448207621209_i18t, 813287860733969412_i18t, 81142660418086_i18t, 830619328460811191_i18t, 61556940512689692_i18t, &
126 51934325451728_i18t, 388641918047049293_i18t, 215058642563049483_i18t, 62467221648435_i18t, 76201727918039944_i18t, 693004732956340691_i18t, 15732444386908_i18t, 125794514089057706_i18t, 229429197107928209_i18t, 55037687525678_i18t, 773091862540744969_i18t, 844508330393682126_i18t, &
127 18336384825330_i18t, 154686196124348767_i18t, 681297534375946515_i18t, 80386287592878_i18t, 490201521685554828_i18t, 717201219257766954_i18t, 78182833757993_i18t, 103614740356856449_i18t, 95527097864797581_i18t, 16726320100436_i18t, 897842553539920931_i18t, 837441497806860984_i18t, &
128 48403098129077_i18t, 791799088218795327_i18t, 364475675590848030_i18t, 87086987551392_i18t, 711854517078544161_i18t, 852424320693150332_i18t, 59959406895756_i18t, 536782107074926966_i18t, 537676326235447210_i18t, 69793950679652_i18t, 694742597709739166_i18t, 693763042633987085_i18t, &
129 41052684708299_i18t, 85211399427365734_i18t, 116182760315001271_i18t, 65378607361501_i18t, 80857009149939512_i18t, 557028198746004375_i18t, 35829035317434_i18t, 717326932123578154_i18t, 982629742552737307_i18t, 94953759765105_i18t, 305946966067683156_i18t, 574377167401875275_i18t, &
130 88902802571733_i18t, 229619176668713819_i18t, 931811048770190271_i18t, 25267680276078_i18t, 3013678680992525_i18t, 463401061632866526_i18t, 36270218540497_i18t, 705585629946580636_i18t, 237993140746255962_i18t, 24074486908231_i18t, 174977792365466257_i18t, 246923322810917141_i18t, &
131 91430288197103_i18t, 288597806669760892_i18t, 938638285025333403_i18t, 34413065578016_i18t, 127815921815005561_i18t, 868836468420090470_i18t, 23053081172816_i18t, 430487623791969842_i18t, 487255036638784583_i18t, 11487696932154_i18t, 902810424020138335_i18t, 124462181441773470_i18t, &
132 63783299490636_i18t, 259666498587618221_i18t, 225225512486764533_i18t, 67720186971698_i18t, 544312419572409913_i18t, 959008952310058822_i18t, 95548255300263_i18t, 520781532296796249_i18t, 481641953868218774_i18t, 76085327132285_i18t, 723110424803456124_i18t, 867697064507995236_i18t, &
133 37774242535411_i18t, 291684276865538926_i18t, 205024910326572967_i18t, 23701913275725_i18t, 675285653248258265_i18t, 463092207058596522_i18t, 29798860272258_i18t, 331913126375147341_i18t, 994889534765745501_i18t, 18495701454879_i18t, 288984856827726077_i18t, 713721403798879715_i18t, &
134 38298203783031_i18t, 473527721580348144_i18t, 513491373226651381_i18t, 34829543829199_i18t, 918180278916522431_i18t, 27392251122869539_i18t, 40957953066405_i18t, 232632538044100059_i18t, 654939159879593635_i18t, 29746152185502_i18t, 371307642255121183_i18t, 693803580388584903_i18t, &
135 41698116222072_i18t, 977186158236678424_i18t, 689157993532961922_i18t, 62467957194401_i18t, 269043877107275048_i18t, 102390895523597457_i18t, 23189706772547_i18t, 915061505504953922_i18t, 979530901129967519_i18t, 86188088225875_i18t, 314529584099251203_i18t, 829009407770775672_i18t, &
136 11306739708304_i18t, 724483816533873502_i18t, 340845647058077308_i18t, 82959174767140_i18t, 363198008187129011_i18t, 875491310547126581_i18t, 97623331044818_i18t, 386269515456334926_i18t, 366572897563400500_i18t, 42846280183517_i18t, 70527831839425882_i18t, 145521227251250327_i18t, &
137 55121603546981_i18t, 200581762165212827_i18t, 652751691296897789_i18t, 32238195734329_i18t, 339946437501907836_i18t, 945765883352399886_i18t, 75506164965184_i18t, 775180738168837861_i18t, 91527357929701337_i18t, 62177842752192_i18t, 623401942399639168_i18t, 44983993173312731_i18t, &
138 32924185707147_i18t, 349566916674687634_i18t, 660915035914677504_i18t, 99518671430235_i18t, 219628894890102423_i18t, 325116913619626622_i18t, 73267460800591_i18t, 547471830798392868_i18t, 535206946944540724_i18t, 76841822524674_i18t, 417161514036427982_i18t, 273348055556214818_i18t, &
139 97142617910342_i18t, 598647204516893989_i18t, 422179826088076852_i18t, 87783646182799_i18t, 346313767754307809_i18t, 363333018982642090_i18t, 10848802521674_i18t, 670883215120185883_i18t, 543223812876952786_i18t, 71329612474782_i18t, 464538636993009049_i18t, 310363619763878039_i18t, &
140 62184073572399_i18t, 794223406235393808_i18t, 339651327408011116_i18t, 66627891981488_i18t, 87797941876876144_i18t, 230030984490851411_i18t, 60661826293682_i18t, 836764744779239180_i18t, 335110989069790714_i18t, 85786944089552_i18t, 990653640447425576_i18t, 83659976645795096_i18t, &
141 66024396409905_i18t, 389607120198219976_i18t, 47599490197230297_i18t, 64913982680032_i18t, 973156037120041377_i18t, 903785566085089252_i18t, 16730939319872_i18t, 750275468906903707_i18t, 539413042652315011_i18t, 94809377245048_i18t, 795150954100921645_i18t, 863754710598436791_i18t, &
142 78639167021187_i18t, 492431995700641917_i18t, 969777599028300699_i18t, 15368713711936_i18t, 614952811305876380_i18t, 278410754449733078_i18t, 40789923115535_i18t, 562561142322423255_i18t, 33685442488917353_i18t, 44889911501440_i18t, 648020369068063960_i18t, 672322193204149535_i18t, &
143 41503128880339_i18t, 536053299340368006_i18t, 977710650566631954_i18t, 81234880673210_i18t, 146739058568557934_i18t, 581403627822703280_i18t, 82616570773948_i18t, 327592232845941706_i18t, 525094512325230608_i18t, 22918802058777_i18t, 319719839450180888_i18t, 72429661980811197_i18t, &
144 77158542502016_i18t, 545090413245809786_i18t, 882778948721859617_i18t, 72107838435069_i18t, 186155435662884062_i18t, 257473692284509516_i18t, 20849603980134_i18t, 1723930671666823_i18t, 555245252804609722_i18t, 53503534226472_i18t, 524250874054075591_i18t, 789781264330331690_i18t /
145
146 do i = 1, 100
147 do j = 1, 3
148 arr(j) = arr(j) + numbers(j, i)
149 end do
150 do j = 3, 2, -1
151 if (arr(j) > ten18) then
152 arr(j - 1) = arr(j - 1) + arr(j) / ten18
153 arr(j) = mod(arr(j), ten18)
154 end if
155 end do
156 end do
157 do while (arr(1) > ten10)
158 arr(1) = arr(1) / 10
159 end do
160 answer = arr(1)
161 end function p0013
162end module Problem0013