知恵袋の五輪マークの円の中にある数の合計の問題をPythonで解いてみた。

 知恵袋の五輪マークの円の中にある数の合計の問題Pythonで解いてみました。(^_^;

 下の図の五輪マークのaからiの9カ所に1から9までの数を1個づつ入れ5個の輪内の数の和が全て等しくなるようにせよ。

  /⌒\/⌒\/⌒\  
 | a  | e  |  i | 
 |  /T\/T\  | 
 | |b|d|f|h| | 
  \⊥/\⊥/\⊥/  
    | c  |  g |    
     \_/\_/     

 そういえば、今年は閏年だし、オリンピックイヤーですね。(^_^;

● OlympicEmblem.py

# coding: UTF-8
# OlympicEmblem.py

import itertools
from time import time

def main():
    tm=time()  # Timer Start
    for p in itertools.permutations(range(1,10)):
        a,b,c,d,e,f,g,h,i = p
        s1 = a+b
        s2 = b+c+d
        s3 = d+e+f
        s4 = f+g+h
        s5 = h+i
        if s1==s2==s3==s4==s5:
            print('%s %s'%(p,s1))

    print("Runtime : %.3f [sec]"%(time()-tm))   # Timer Stop & Disp

if __name__ == '__main__':
    main()

●実行結果

(4, 9, 1, 3, 8, 2, 5, 6, 7) 13
(5, 9, 2, 3, 4, 7, 1, 6, 8) 14
(7, 6, 5, 2, 3, 8, 1, 4, 9) 13
(7, 6, 5, 2, 8, 3, 1, 9, 4) 13
(8, 3, 7, 1, 6, 4, 5, 2, 9) 11
(8, 6, 1, 7, 4, 3, 2, 9, 5) 14
(9, 2, 5, 4, 6, 1, 7, 3, 8) 11
(9, 4, 1, 8, 3, 2, 5, 6, 7) 13
Runtime : 0.265 [sec]

 ちなみに、この問題には左右対称性があって、上から、1番目と4番目、2番目と6番目、3番目と8番目、5番目と7番目はそれぞれ逆順になっています。

※参考URL
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1059414445
オリンピックは、閏年(うるうどし)に開催されると勘違いしていませんか? - 日本気象協会 tenki.jp