{"id":1843,"date":"2012-04-07T22:33:00","date_gmt":"2012-04-07T02:33:00","guid":{"rendered":""},"modified":"2014-01-08T10:52:24","modified_gmt":"2014-01-08T02:52:24","slug":"project-euler26-50","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1843.html","title":{"rendered":"Project Euler[26-50]"},"content":{"rendered":"<p>26\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encoding=utf-8\r\n\u201d\u2019\r\n\u800c\u600e\u4e48\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5faa\u73af\u957f\u5ea6\u5462\r\n\u53ea\u9700\u8981\u77e5\u9053\u5b83\u80fd\u88ab\u591a\u5c11\u957f\u5ea6\u76849\u6574\u9664\u5c31\u884c\u4e86\r\n3\u80fd\u88ab9\u6574\u9664\uff0c\u6240\u4ee5\u5b83\u7684\u5faa\u73af\u957f\u5ea6\u662f1\r\n7\u80fd\u88ab999999\u6574\u9664\uff0c\u5546\u6b63\u597d\u662f\u5faa\u73af\u4f53142857\uff0c\u6240\u4ee5\u5b83\u7684\u5faa\u73af\u957f\u5ea6\u662f6\r\n\u201d\u2019\r\nfrom euler import is_prime\r\n\r\nn = 997\r\nfor p in range(n, 1, -2):\r\n    if not is_prime(p):\r\n        continue\r\n    c = 1\r\n    while (pow(10, c) \u2013 1) % p != 0:\r\n        c += 1\r\n    if p-c == 1:\r\n        break\r\nprint p\r\n<\/pre>\n<p>27\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encoding=utf-8\r\nfrom euler import is_prime\r\n\r\ndef get_primes_num(a, b):\r\n    n = 0\r\n    while 1:\r\n        value = n*n + a*n +b\r\n        if value &lt;= 0:\r\n            break\r\n        if not is_prime(value):\r\n            break\r\n        n += 1\r\n    return n\r\n\r\n#print get_primes_num(-79, 1601)\r\n\u201d\u2019\r\nb\u4e00\u5b9a\u662f\u6b63\u7d20\u6570\r\n\u56e0\u4e3a\u5f53n\u4e3a0\u65f6\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u4e3ab\r\n\u201d\u2019\r\nprimes_b = &#x5B;]\r\nfor i in range(3, 1000):\r\n    if is_prime(i):\r\n        primes_b.append(i)\r\n\r\nsolution = ()\r\nsnum = 0\r\nfor a in range(-999, 1000):\r\n    for b in primes_b:\r\n        gpnum = get_primes_num(a, b)\r\n        if gpnum &gt; snum:\r\n            solution = (a, b)\r\n            snum = gpnum\r\n            print solution, gpnum, solution&#x5B;0] * solution&#x5B;1]\r\n<\/pre>\n<p>28\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encoding=utf-8\r\n\r\nnumbers_sum = 1\r\ncorder_sum = 1\r\ncircle_num = (1001 + 1) \/ 2   #2n-1\u7b49\u4e8e\u8fb9\u957f\uff0cn\u4e3a\u5708\u6570\r\nfor circle in range(2, circle_num+1):\r\n    numbers_sum = (2 * (circle-1) \u2013 1)**2\r\n    first_num = numbers_sum + 1 #\u8fd9\u4e00\u5708\u7684\u7b2c\u4e00\u4e2a\u6570\r\n    step = 2*circle \u2013 2  #\u8fb9\u957f-1\r\n    first_corder = first_num + step \u2013 1\r\n    corder_sum += first_corder*4 + step*6\r\n\r\nprint corder_sum\r\n<\/pre>\n<p>29\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nresult = set()\r\nfor a in range(2, 101):\r\n    for b in range(2, 101):\r\n        result.add(a**b)\r\n\r\nprint len(result)\r\n<\/pre>\n<p>30\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nresultsum = &#x5B;]\r\n\r\nfor num in range(2, 9**5*7):\r\n    tmpsum = 0\r\n    for i in str(num):\r\n        tmpsum += int(i)**5\r\n    if tmpsum == num:\r\n        resultsum.append(tmpsum)\r\nprint resultsum\r\nprint sum(resultsum)\r\n<\/pre>\n<p>31\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encodin=utf-8\r\ntarget = 200\r\ncoins = &#x5B;1,2,5,10,20,50,100,200]\r\nways = &#x5B;1]+&#x5B;0]*target\r\n\u201d\u2019\r\n\u52a8\u6001\u5f52\u5212\u95ee\u9898\r\n\u201d\u2019\r\nfor coin in coins:\r\n   for i in range(coin, target+1):\r\n       ways&#x5B;i] += ways&#x5B;i-coin]\r\nprint ways&#x5B;target]\r\n<\/pre>\n<p>32\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npandigitals = set()\r\nfor multiplicard in range(1, 5001):\r\n    for multiplier in range(1, 101):\r\n        product = multiplicard * multiplier\r\n        ss = str(product)+str(multiplicard)+str(multiplier)\r\n        ss = \u201d.join(sorted(ss))\r\n        if ss == \u2019123456789\u2032:\r\n            print product\r\n            pandigitals.add(product)\r\n\r\nprint sum(pandigitals)\r\n<\/pre>\n<p>33\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nd = 1\r\nfor i in range(1, 10):\r\n  for j in range(1, i):\r\n    for k in range(1, j):\r\n      ki = k*10 + i\r\n      ij = float(i)*10 + j\r\n      if (k*ij == ki*j):\r\n        d *= ij\/ki\r\nprint d\r\n<\/pre>\n<p>34\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfact = (1,1,2,6,24,120,720,5040,40320,362880)\r\ns = 0\r\n\r\nfor n in range(10, 50000):\r\n    x = sum( fact&#x5B;int(d)] for d in str(n) )\r\n    if n == x:\r\n        s += n\r\nprint s\r\n<\/pre>\n<p>35\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport math\r\ntotal=1\r\n\r\ndef prime(n):\r\n    if(n==2):\r\n        return True\r\n    if(not n%2):\r\n        return False\r\n    else:\r\n        for i in range(3,int(math.sqrt(n))+1,2):\r\n            if(not n%i):\r\n                return False\r\n    return True\r\n\r\ndef rotate(l):\r\n    l.insert(0,l.pop())\r\n    return (check(l))\r\n\r\ndef check(l):\r\n    n=0\r\n    for k in l:\r\n        n=n*10+int(k)\r\n    if(prime(n)):\r\n        return True\r\n    return False\r\n\r\nfor i in range(3,1000000,2):\r\n    flag=True\r\n    evenflag=False\r\n    even=&#x5B;2,4,6,8,0]\r\n    l=list(str(i))\r\n    for k in even:\r\n        if(str(k) in l):\r\n            evenflag=True\r\n            break;\r\n    if(evenflag):\r\n        continue\r\n    for k in range(len(l)):\r\n        if(not rotate(l)):\r\n            flag=False\r\n            break\r\n    if(flag):\r\n        total+=1\r\nprint total\r\n<\/pre>\n<p>36\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nresult = 0\r\nfor i in range(1, 1000001):\r\n    if str(i) == str(i)&#x5B;::-1]:\r\n        b = str(bin(i))&#x5B;2:]\r\n        if b == b&#x5B;::-1]:\r\n            #print i, b\r\n            result += i\r\nprint result\r\n<\/pre>\n<p>37\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom euler import is_prime\r\nfrom math import log10\r\n\r\nprimes = &#x5B;23,37,53,73,313,317,373,797,3137,3797,7937,31397,31973,37313,37397,71317,\r\n71713,71917,73973,79397,313717,317197,319313,371737,371797,373717,373937,\r\n379397,713737,713917,717317,717397,717917,719197,719713,719717,731713,\r\n731737,739373,739397,791317,791797,793717,797917]\r\n\r\ndef trunc(n):\r\n  c = n\r\n  while c&gt;10:\r\n    c = c % ( 10**(int(log10(c))) )\r\n    n = n\/\/10\r\n    if not is_prime(c) or not is_prime(n): return False\r\n  return True\r\n\r\nc = 0\r\ns = 0\r\nfor p in primes:\r\n  if trunc(p):\r\n    print p\r\n    c += 1\r\n    s += p\r\n\r\nprint c,&quot;primes found for a sum of&quot;,s\r\n<\/pre>\n<p>38\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef f1(n):\r\n    strNum = str(n)\r\n    for i in range(1,5):\r\n        tmpStr = strNum\r\n        num = int(strNum&#x5B;:i])\r\n        index = 1\r\n        flag = False\r\n        while True:\r\n            str1 = str(num*index)\r\n            len1 = len(str1)\r\n            if str1 == tmpStr&#x5B;:len1]:\r\n                if len(tmpStr)&gt;len1:\r\n                    tmpStr=tmpStr&#x5B;len1:]\r\n                elif len(tmpStr) == len1:\r\n                    flag = True\r\n                    break\r\n            else:\r\n                break\r\n            index += 1\r\n        if flag:\r\n            return True\r\n    return False\r\n\r\nnumList=&#x5B;1,&#x5B;'','12345678']]\r\nwhile True:\r\n    if numList&#x5B;0]==40320:\r\n    #8!=362880\r\n        break\r\n    for i in range(1,numList&#x5B;0]+1):\r\n        left = numList&#x5B;i]&#x5B;0]\r\n        right= numList&#x5B;i]&#x5B;1]\r\n        l = left+right&#x5B;0]\r\n        r = right.replace(right&#x5B;0],\u201d)\r\n        numList&#x5B;i] = &#x5B;l,r]\r\n        for j in right&#x5B;1:]:\r\n            l = left+j\r\n            r = right.replace(j,\u201d)\r\n            numList.append(&#x5B;l,r])\r\n            numList&#x5B;0] += 1\r\nnumStrList = &#x5B;]\r\nfor i in numList&#x5B;1:]:\r\n    numStrList.append(\u20199\u2032+i&#x5B;0]+i&#x5B;1])\r\ndel numList\r\n# print numStrList\r\n# print len(numStrList)\r\nresult = 0\r\nfor i in numStrList:\r\n    if f1(int(i)):\r\n        if int(i) &gt; result:\r\n            result = int(i)\r\nprint result\r\n<\/pre>\n<p>39\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nt_max = 0\r\np_limit = 1000\r\n\r\nfor p in range(p_limit\/\/2, p_limit+1, 2):\r\n  t = 0;\r\n  for a in range(2, p\/4+1):\r\n    if  p*(p \u2013 2*a) % (2*(p-a)) == 0: t += 1\r\n    if t &gt; t_max: (t_max, p_max) = (t, p)\r\n\r\nprint p_max\r\n<\/pre>\n<p>40\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nnum = 0\r\nstrs = \u201d\r\ncount = 0\r\nwhile count &lt; 1000001:\r\n    l = len(str(num))\r\n    count += l\r\n    strs += str(num)\r\n    num += 1\r\n\r\nss = 1\r\nfor i in range(7):\r\n    ss *= int( strs&#x5B;10**i] )\r\nprint ss\r\n<\/pre>\n<p>41\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom euler import is_prime, is_pandigital\r\n\u201d\u2019\r\nany integer is divisible by 3 or 9 whose sum of digits is divisible by 3 or 9; therefore composite and not prime.\r\n\r\n9+8+7+6+5+4+3+2+1 = 45,\r\n8+7+6+5+4+3+2+1 = 36,\r\n6+5+4+3+2+1 = 21, and\r\n5+4+3+2+1 = 15,\r\nall of which are divisible by 3 and therefore could not yield a 1 to {5, 6, 8, 9} pandigital prime. So that leaves 4 and 7 digit prime numbers less than 4321 and 7654321 respectively.\r\n\r\n\u201d\u2019\r\n\r\nn = 7654321\r\nwhile not(is_prime(n) and is_pandigital(str(n), 7)):\r\n    n -= 2\r\nprint n\r\n<\/pre>\n<p>42\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport urllib\r\n\r\nwords = urllib.urlopen(\u2018http:\/\/projecteuler.net\/project\/words.txt\u2019).read()\r\nwordslist = &#x5B; w&#x5B;1:-1] for w in words.split(\u2018,\u2019) ]\r\ntri_numbers = &#x5B;]\r\nn = 1\r\nwhile n*(n+1) \/ 2 &lt;= 260:\r\n    tri_numbers.append(n*(n+1)\/2)\r\n    n += 1\r\nresult = 0\r\nfor word in wordslist:\r\n    v = 0\r\n    for w in word:\r\n        v += ord(w)-ord(\u2018A\u2019)+1\r\n    if v in tri_numbers:\r\n        result += 1\r\n\r\nprint result\r\n<\/pre>\n<p>43\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport itertools\r\n#here is a better and faster way: http:\/\/blog.dreamshire.com\/2009\/04\/28\/project-euler-problem-43-solution\/\r\niter_list = itertools.permutations(range(10))\r\nresult_sum = 0\r\ndivs = &#x5B;2,3,5,7,11,13,17]\r\nfor number in iter_list:\r\n    if number&#x5B;0] == 0: continue\r\n    number_str = \u201d.join(&#x5B;str(n) for n in number])\r\n    isok = True\r\n    for i in range(len(divs)):\r\n        if int(number_str&#x5B;i+1: i+4]) % divs&#x5B;i] != 0:\r\n            isok = False\r\n            break\r\n    if isok:\r\n        result_sum += int(number_str)\r\nprint result_sum\r\n<\/pre>\n<p>44\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npent = set( n * (3*n \u2013 1) \/ 2 for n in range(2,2400) )\r\n\r\ndef pe44():\r\n  for pj in pent:\r\n    for pk in pent:\r\n      if pj \u2013 pk in pent and pj + pk in pent:\r\n        return pj \u2013 pk\r\n\r\nprint pe44()\r\n<\/pre>\n<p>45\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#encoding=utf8\r\nfrom math import sqrt\r\n\u201d\u2019\r\nNote that Hexagonal numbers are a subset of Triangle numbers so we only determine the first occurrence of Hi = Pj to find our answer. We can safely start our search from 144 as alluded from the problem\u2019s description.\r\n\u201d\u2019\r\ndef is_pentagonal(n):\r\n       p = (sqrt(1 + 24*n) + 1) \/ 6\r\n       return p==int(p)\r\nh = lambda n: n*(2*n \u2013 1) #calculate the nth hexagonal number\r\nn = 144\r\nwhile not(is_pentagonal(h(n))): n += 1\r\nprint h(n)\r\n<\/pre>\n<p>46\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nn = 5\r\nf = 1\r\nprimes = set()\r\n\r\nwhile (1):\r\n  if all( n % p for p in primes ):\r\n    primes.add(n)\r\n  else:\r\n    if not any( (n-2*i*i) in primes for i in range(1, n) ):\r\n      break\r\n  n += 3-f\r\n  f = -f\r\nprint n\r\n<\/pre>\n<p>47\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom euler import factor\r\n# the euler module can find here : http:\/\/blog.dreamshire.com\/2009\/03\/26\/94\/\r\nci = 1\r\nnf = 4        #number of distinct factors\r\nns = 4        #number of consecutive integers\r\nn = 2*3*5*7    #starting candidate for search\r\nwhile ci != ns:\r\n  n += 1\r\n  if len(factor(n)) == nf: ci += 1\r\n  else: ci = 0\r\n\r\nprint &quot;Answer to PE47 = &quot;, n-nf+1\r\n<\/pre>\n<p>48\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nresult = 0\r\nfor i in range(1, 1001):\r\n    result += i**i\r\nprint str(result)&#x5B;-10:]\r\n<\/pre>\n<p>49\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom euler import is_prime, is_perm\r\n\r\nn = 1489     # must be odd\r\nwhile True:\r\n  b, c = n+3330, n+6660\r\n  if is_prime(n) and is_prime(b) and is_prime(c) \\\r\n    and is_perm(n,b) and is_perm(b,c): break\r\n  n += 2\r\n\r\nprint &quot;Answer to PE49 = &quot;, str(n)+str(b)+str(c)\r\n<\/pre>\n<p>50\u3001<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom euler import prime_sieve, is_prime\r\n\r\nmax = 1000000\r\nprimes = prime_sieve(max)\r\nprime_sum = &#x5B;0]\r\n\r\nsum = 0\r\ncount = 0\r\nwhile(sum &lt; max):\r\n    sum += primes&#x5B;count]\r\n    prime_sum.append(sum)\r\n    count += 1\r\n\r\nterms = 1\r\nfor i in range(count):\r\n    for j in range(i+terms, count):\r\n        n = prime_sum&#x5B;j] \u2013 prime_sum&#x5B;i]\r\n        if j-i &gt; terms and is_prime(n):\r\n            terms, max_prime = j-i, n\r\nprint max_prime\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>26\u3001 #encoding=utf-8 \u201d\u2019 \u800c\u600e\u4e48\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5faa\u73af\u957f\u5ea6\u5462 \u53ea\u9700\u8981\u77e5\u9053\u5b83\u80fd\u88ab\u591a\u5c11\u957f\u5ea6\u76849\u6574\u9664\u5c31 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1843","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1843"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1843\/revisions"}],"predecessor-version":[{"id":5447,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1843\/revisions\/5447"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}