{"id":900,"date":"2013-12-04T12:24:00","date_gmt":"2013-12-04T17:24:00","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=900"},"modified":"2013-12-04T12:24:00","modified_gmt":"2013-12-04T17:24:00","slug":"project-euler-12","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=900","title":{"rendered":"Project Euler 12"},"content":{"rendered":"<p>So I finished problem 12 from <a href=\"http:\/\/projecteuler.net\/problem=12\" target=\"_blank\">Project Euler<\/a>. I made it via a brute force attack in <a href=\"http:\/\/www.perl.org\" target=\"_blank\">Perl<\/a>.<\/p>\n<p>[perl]<br \/>\n#!\/usr\/bin\/env perl<\/p>\n<p>use warnings;<br \/>\nuse strict;<\/p>\n<p>my $j = 0;<br \/>\nfor (my $x=1; $x &lt;= 1000000; $x++) {<br \/>\n        $j = $j + $x;<br \/>\n        my $dividend = 0;<br \/>\n        print &quot;j is $j, x is $x and dividend is $dividend.\\n&quot;;<br \/>\n        for (my $z=1; $z &lt;= ($j + 1); $z++) {<br \/>\n                if ($j%$z == 0) {<br \/>\n                        $dividend = $dividend + 1;<br \/>\n                        print &quot;$z is dividend of $j and $j has dividend $dividend.\\n&quot;;<br \/>\n                        if ($dividend &gt; 501) {<br \/>\n                                print &quot;$z is dividend of $j and $j has dividend $dividend.\\n&quot;;<br \/>\n                                exit; }<br \/>\n                                }<br \/>\n                        }<br \/>\n                }<br \/>\n[\/perl]<\/p>\n<p>and with some logic in <a href=\"http:\/\/www.python.org\" target=\"_blank\">Python<\/a>. The python script can be divided in three part, first we find the triangle number, then we calculate the numbers that factor the triangle number and place it into an array and the last step would be to calculate how many divisors the triangle number has.<\/p>\n<p>[python]<br \/>\n#!\/usr\/bin\/python <\/p>\n<p>import math<\/p>\n<p># Start the triangle by one<br \/>\ntriangle = 0<br \/>\nfor x in range(1, 100000):<br \/>\n        triangle = x*(x+1)\/2<br \/>\n        #print &quot;triangles is &quot;, triangle<\/p>\n<p># We factor the triangle into prime numbers<br \/>\n        def primefactors(x):<br \/>\n                factorlist=[]<br \/>\n                loop=2<br \/>\n                while loop&lt;=x:<br \/>\n                        if x%loop==0:<br \/>\n                                x\/=loop<br \/>\n                                factorlist.append(loop)<br \/>\n                        else:<br \/>\n                                loop+=1<br \/>\n                return factorlist<br \/>\n        #print primefactors(triangle)<\/p>\n<p># We calculate the number of divisors of the triangle number<br \/>\n        divisor = 1<br \/>\n        for z in set(primefactors(triangle)):<br \/>\n                #print &quot;{0}\\t{1}&quot;.format(z,primefactors(triangle).count(z))<br \/>\n                divisor = (primefactors(triangle).count(z) + 1) * divisor<br \/>\n                #print &quot;Triangle&quot;, triangle,&quot; has &quot;, divisor,&quot; divisors.&quot;<br \/>\n                if (divisor &gt; 500):<br \/>\n                        print &quot;Triangle&quot;, triangle,&quot; has &quot;, divisor,&quot; divisors.&quot;<br \/>\n                        quit()<br \/>\n[\/python]<\/p>\n<p>Reference:<br \/>\n1) <a href=\"http:\/\/en.wikipedia.org\/wiki\/Integer_factorization\" target=\"_blank\">Integer Factorization<\/a><br \/>\n2) <a href=\"http:\/\/www.mathsisfun.com\/algebra\/triangular-numbers.html\" target=\"_blank\">Triangular numbers<\/a><br \/>\n3) <a href=\"http:\/\/mathschallenge.net\/library\/number\/number_of_divisors\" target=\"_blank\">Number of divisors of a number<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I finished problem 12 from Project Euler. I made it via a brute force attack in Perl. [perl] #!\/usr\/bin\/env perl use warnings; use strict; my $j = 0; for (my $x=1; $x &lt;= 1000000; $x++) { $j = $j + $x; my $dividend = 0; print &quot;j is $j, x is $x and dividend [&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,3],"tags":[50,67,70],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-ew","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/900"}],"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=900"}],"version-history":[{"count":5,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}