{"id":978,"date":"2015-05-06T05:20:27","date_gmt":"2015-05-06T09:20:27","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=978"},"modified":"2016-04-14T11:39:32","modified_gmt":"2016-04-14T15:39:32","slug":"project-euler-number-11","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=978","title":{"rendered":"Project Euler number 11"},"content":{"rendered":"<p>Below is my solution to <a href=\"https:\/\/projecteuler.net\/problem=11\" target=\"blank_\">Poject Euler number eleven<\/a>. I created back ordered array with the number and its positions on a 2D 20&#215;20 array. The 4 corners 4&#215;4 array products only the vertical and horizontal products where calculated since the diagonal products are calculated previously with the 16&#215;16 array.<\/p>\n<p>[python]<br \/>\n#!\/usr\/bin\/env python<\/p>\n<p>import time<\/p>\n<p>start = time.time()<br \/>\nf = open(&#8220;grid.txt&#8221;, &#8220;r&#8221;)<br \/>\ni = 0<br \/>\nj = 0<br \/>\nnewList = []<br \/>\nList = []<\/p>\n<p># Create and array 20&#215;20 converting members to int<br \/>\nfor line in f:<br \/>\n        List.append(line.split())<br \/>\n        List[i] = map(int, List[i])<br \/>\n        i = i + 1<\/p>\n<p># Create an ordered array starting by highest number and writing also position<br \/>\ni = 0<br \/>\nj = 0<br \/>\nfor i in range(len(List[i])):<br \/>\n        for j in range(len(List[i])):<br \/>\n                newList.append([List[i][j],[i,j]])<br \/>\nsortedList = sorted(newList)<br \/>\n# backorderdList is an array containing an ordered number with its positions in array second entry.<br \/>\nbackorderedList = sortedList[::-1]<\/p>\n<p>max = 0<br \/>\nfor member in backorderedList:<br \/>\n        if ((member[1][0] == 0) and (member[1][1] == 0)):<br \/>\n                # Multiply upper corner first four lines<br \/>\n                for x in range(4):<br \/>\n                        # Line multiplication<br \/>\n                        newmax = List[x][0]*List[x][1]*List[x][2]*List[x][3]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<br \/>\n                        # Column multiplication<br \/>\n                        newmax = List[0][x]*List[1][x]*List[2][x]*List[3][x]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<\/p>\n<p>        elif ((member[1][0] == 0) and (member[1][1] == 16)):<br \/>\n                # Multiply upper right corner<br \/>\n                for x in range(4):<br \/>\n                        # Line multiplication<br \/>\n                        newmax = List[x][16]*List[x][17]*List[x][18]*List[x][19]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<br \/>\n                        # Column multiplication<br \/>\n                        newmax = List[0][x+16]*List[1][x+16]*List[2][x+16]*List[3][x+16]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<\/p>\n<p>        elif ((member[1][0] == 16) and (member[1][1] == 0)):<br \/>\n                # Multiply lower left corner<br \/>\n                for x in range(4):<br \/>\n                        # Line multiplication<br \/>\n                        newmax = List[x+16][0]*List[x+16][1]*List[x+16][2]*List[x+16][3]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<br \/>\n                        # Column multiplication<br \/>\n                        newmax = List[16][x]*List[17][x]*List[18][x]*List[19][x]<br \/>\n                        if (newmax > max):<br \/>\n                                print max<\/p>\n<p>        elif ((member[1][0] == 16) and (member[1][1] == 16)):<br \/>\n                # Multiply lower right corner<br \/>\n                for x in range(4):<br \/>\n                        # Line multiplication<br \/>\n                        newmax = List[x+16][16]*List[x+16][17]*List[x+16][18]*List[x+16][19]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<br \/>\n                        # Column multiplication<br \/>\n                        newmax = List[16][x+16]*List[17][x+16]*List[18][x+16]*List[19][x+16]<br \/>\n                        if (newmax > max):<br \/>\n                                max = newmax<\/p>\n<p>        elif ((member[1][0] >= 3) and (member[1][0] <= 16) and (member[1][1] >= 3) and (member[1][1] <= 16)):\n                # column numbers from numbers not on edges\n                newmax = List[member[1][0]-3][member[1][1]]*List[member[1][0]-2][member[1][1]]*List[member[1][0]-1][member[1][1]]*List[member[1][0]][member[1][1]]\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                newmax = List[member[1][0]+3][member[1][1]]*List[member[1][0]+2][member[1][1]]*List[member[1][0]+1][member[1][1]]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                # line numbers<br \/>\n                newmax = List[member[1][0]][member[1][1]-3]*List[member[1][0]][member[1][1]-2]*List[member[1][0]][member[1][1]-1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                newmax = List[member[1][0]][member[1][1]+3]*List[member[1][0]][member[1][1]+2]*List[member[1][0]][member[1][1]+1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                # diagonal numbers<br \/>\n                newmax = List[member[1][0]-3][member[1][1]-3]*List[member[1][0]-2][member[1][1]-2]*List[member[1][0]-1][member[1][1]-1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                newmax = List[member[1][0]+3][member[1][1]+3]*List[member[1][0]+2][member[1][1]+2]*List[member[1][0]+1][member[1][1]+1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                newmax = List[member[1][0]-3][member[1][1]+3]*List[member[1][0]-2][member[1][1]+2]*List[member[1][0]-1][member[1][1]+1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<br \/>\n                newmax = List[member[1][0]+3][member[1][1]-3]*List[member[1][0]+2][member[1][1]-2]*List[member[1][0]+1][member[1][1]-1]*List[member[1][0]][member[1][1]]<br \/>\n                if (newmax > max):<br \/>\n                        max = newmax<\/p>\n<p>print &#8220;Max is: %d.&#8221; % (max)<br \/>\nelapsed = (time.time() &#8211; start)<br \/>\nprint &#8220;Elapsed time: %s seconds.&#8221; % (elapsed)<\/p>\n<p>f.close()<br \/>\n[\/python]<\/p>\n<p>Took the execution time idea from <a href=\"http:\/\/code.jasonbhill.com\/python\/project-euler-problem-11\/\" target=\"blank_\">here<\/a>.<\/p>\n<p>Execution:<br \/>\n[shell]<br \/>\nuser@server1: ~ $ python euler_11.py ; uname -a<br \/>\nMax is: 70600674.<br \/>\nElapsed time: 0.0441608428955 seconds.<br \/>\nLinux server1 2.6.32-042stab106.4 #1 SMP Fri Mar 27 15:19:28 MSK 2015 x86_64 GNU\/Linux<br \/>\nuser@server1: ~ $<br \/>\n[\/shell]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is my solution to Poject Euler number eleven. I created back ordered array with the number and its positions on a 2D 20&#215;20 array. The 4 corners 4&#215;4 array products only the vertical and horizontal products where calculated since the diagonal products are calculated previously with the 16&#215;16 array. [python] #!\/usr\/bin\/env python import time [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[62,63],"tags":[67],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-fM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/978"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=978"}],"version-history":[{"count":6,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions"}],"predecessor-version":[{"id":1183,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions\/1183"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}