1
1

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 34: 各桁の階乗の和

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

問題 34:各桁の階乗の和

原文 Problem 34: Digit factorials

問題の要約:各桁の階乗の和が元の数と同じになる数の合計を求めよ

これも上限が問題になりますが、7桁を超えると全桁が9でも階乗の和は7桁にならないので$9! \times 7$まで調べればOKです。

import math
fct = [math.factorial(d) for d in range(10)]
def digFactSum(n):
  return sum(fct[int(d)] for d in str(n))

print(f"Answer = {sum([n for n in range(11,fct[9]*7) if n == digFactSum(n)])}")

(開発環境:Google Colab)

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?