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 80: 平方根の小数展開

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

問題 80.平方根の小数展開

原文 Problem 80: Square root digital expansion

問題の要約:$1 \le n \le 100$の自然数の平方根が無理数になるものを小数で表したときその最初の100桁(整数部を含む)の数字和の合計を求めよ

平方根の値の精度を変えるためにでdecimal.getcontext().precを使いました。

import decimal
decimal.getcontext().prec = 120  # 120 digit pre square root precision
def sumsqrt100(n): # the digital sums of the first one hundred decimal digits 
  return sum([int(str(decimal.Decimal(n).sqrt()).replace(".","")[i]) for i in range(100)])

print(f"Answer: {sum([sumsqrt100(n) for n in range(1,101) if int(n**(1/2))**2 != n])}")  

(開発環境: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?