0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Project Euler】Problem 45: 三角・五角・六角数

Last updated at Posted at 2022-01-22
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて回答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします。

問題 45. 三角・五角・六角数

原文 Problem 45: Triangular, pentagonal, and hexagonal

問題の要約:40755の次の三角・五角・六角数の共通の数を求めよ

Problem 44: 五角数と同様に各々の定義から2次関数を解いて三角・五角数判定関数を作ります。

import math
import itertools

def isTri(x):
	f = (-1.0 + math.sqrt(1+8*x))/2
	return (f - int(f) == 0)
 
def isPent(x):
	f = (.5 + math.sqrt(.25+6*x))/3
	return (f - int(f) == 0)


for n in itertools.count(143+1):
  hexa = n * (2*n-1)
  if isTri(hexa) and isPent(hexa):
    break
print(f"Answer: {hexa}")

(開発環境:Google Colab)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?