Symbol(clack:cancel)
This commit is contained in:
45
solutions/0000_0029/0023/euler_23_s2.py
Normal file
45
solutions/0000_0029/0023/euler_23_s2.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import time
|
||||
|
||||
from sympy import divisors
|
||||
|
||||
|
||||
def timer(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
start_time = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end_time = time.time()
|
||||
print(f"Execution time: {end_time - start_time:.6f} seconds")
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def is_abundant(n: int) -> bool:
|
||||
return sum(divisors(n)) > 2 * n
|
||||
|
||||
|
||||
def is_sum_of_two_abundants(n: int) -> bool:
|
||||
if n < 24:
|
||||
return False
|
||||
for i in range(12, n // 2 + 1):
|
||||
if is_abundant(i) and is_abundant(n - i):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@timer
|
||||
def main():
|
||||
limit = 28123
|
||||
abundant_numbers = [n for n in range(1, limit + 1) if is_abundant(n)]
|
||||
non_abundant_sums = set(range(1, limit + 1))
|
||||
for i in range(len(abundant_numbers)):
|
||||
for j in range(i, len(abundant_numbers)):
|
||||
if abundant_numbers[i] + abundant_numbers[j] > limit:
|
||||
break
|
||||
non_abundant_sums.discard(abundant_numbers[i] + abundant_numbers[j])
|
||||
print(sum(non_abundant_sums))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user