[Project Euler]Problem 45

來源:互聯網
上載者:User

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle
Tn=n(n+1)/2
1, 3, 6, 10, 15, ...

Pentagonal
Pn=n(3n1)/2
1, 5, 12, 22, 35, ...

Hexagonal
Hn=n(2n1)
1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

 

找出下一個數既是三角數又是五角數,而且還是六角數。

這道題有了上一題的經驗就解的比較容易了

t = [i*(i+1)//2 for i in range(286, 1000001)]p = [i*(3*i-1)//2 for i in range(166, 1000001)]st = set(t)sp = set(p)n = 144while True:    h = n*(2*n - 1)    if h in st and h in sp:        print(h)        break    else:        n += 1

 

速度很快,基本0.s就出來。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.