Sudal's Garage

Project Euler 23 본문

Programming/Project Euler - python

Project Euler 23

_Sudal 2019. 3. 11. 05:00

Question: 


A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


문제:


자신을 제외한 약수(진약수)를 모두 더하면 자기 자신이 되는 수를 완전수라고 합니다.
예를 들어 28은 1 + 2 + 4 + 7 + 14 = 28 이므로 완전수입니다.
또, 진약수의 합이 자신보다 작으면 부족수, 자신보다 클 때는 초과수라고 합니다.

12는 1 + 2 + 3 + 4 + 6 = 16 > 12 로서 초과수 중에서는 가장 작습니다.
따라서 초과수 두 개의 합으로 나타낼 수 있는 수 중 가장 작은 수는 24 (= 12 + 12) 입니다.

해석학적인 방법을 사용하면, 28123을 넘는 모든 정수는 두 초과수의 합으로 표현 가능함을 보일 수가 있습니다.
두 초과수의 합으로 나타낼 수 없는 가장 큰 수는 실제로는 이 한계값보다 작지만, 해석학적인 방법으로는 더 이상 이 한계값을 낮출 수 없다고 합니다.

그렇다면, 초과수 두 개의 합으로 나타낼 수 없는 모든 양의 정수의 합은 얼마입니까?


Solution:

from sympy import divisors
from itertools import combinations_with_replacement as comb
import time

numbers = set(range(1,28124))

def abundant(n):
    if sum(divisors(n)) > 2 * n:
        return True
    return False

start = time.time()
abun = list()
for i in range(1,int(28123 - 12)):
    if abundant(i):
        abun.append(i)
        
a = list(set(numbers - set(map(sum,comb(abun,2)))))
a.sort()
print(sum(a))
print("Found in ...{:.2f}s".format(time.time() - start))



함수 abundant 는 n 이 초과수인지 아닌지 판별해준다.

초과수이면 True, 아니면 False.


초과수들을 모두 저장한 후, combination 을 이용해 초과수들 2개를 합한 수들을 a 에 저장한다

중복을 제거 한후, 그 합을 출력한다.


3.48s 걸렸다



'Programming > Project Euler - python' 카테고리의 다른 글

Project Euler 25  (0) 2019.03.13
Project Euler 24  (0) 2019.03.12
Project Euler 22  (0) 2019.03.10
Project Euler 21  (0) 2019.03.09
Project Euler 20  (0) 2019.03.08