4
2

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 5 years have passed since last update.

Project Euler 21 「友愛数」

Posted at

小出しスタイルにシフトチェンジ。

Problem 21 「友愛数」

d(n) を n の真の約数の和と定義する. (真の約数とは n 以外の約数のことである. )
もし, d(a) = b かつ d(b) = a (a ≠ b のとき) を満たすとき, a と b は友愛数(親和数)であるという.
例えば, 220 の約数は 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 なので d(220) = 284 である.
また, 284 の約数は 1, 2, 4, 71, 142 なので d(284) = 220 である.
それでは10000未満の友愛数の和を求めよ.

def hoge(num):
    ans = 0
    shinyakuwa = {}
    for n in xrange(1, num):
        shinyakuwa[n] = get_shinyakuwa(n)
    for x, y in shinyakuwa.iteritems():
        if  x != y \
        and shinyakuwa.has_key(y) \
        and x == shinyakuwa[y]:
            ans += x
    return ans

def get_shinyakuwa(n):
    shinyaku = [1]
    for m in xrange(2, (n/2)+1):
        if n % m == 0:
            shinyaku.append(m)
    return sum(shinyaku)

print hoge(10000)

何か閃きそうだったけどピンとこなかったのでいつもの愚直スタイル。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?