Help us understand the problem. What is going on with this article?

Project Euler 21 「友愛数」

More than 1 year has passed since last update.

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

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)

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away