LoginSignup
203
112

More than 1 year has passed since last update.

約数を高速で列挙するコード(Python)

Last updated at Posted at 2019-01-01

約数を高速で列挙するコードです(計算量$O(\sqrt{N})$)
問題の制約が$10^9$でも間に合います。
ほぼ自分用のメモとして投稿

(@yucatioさんの指摘を受け,小さい約数のリストと大きい約数のリストを最後に結合する方法に変えました.)

コード

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
203
112
4

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
203
112