From 20893290f45f66f18a0634899903146da7f1b48e Mon Sep 17 00:00:00 2001 From: daviddoji Date: Wed, 28 Sep 2022 20:22:13 +0200 Subject: [PATCH] Adopted new convention from template --- src/Python/Problems001-050/Problem023.py | 30 ++++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Python/Problems001-050/Problem023.py b/src/Python/Problems001-050/Problem023.py index a860df9..50d3891 100644 --- a/src/Python/Problems001-050/Problem023.py +++ b/src/Python/Problems001-050/Problem023.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python """ Created on 05 Jan 2019 @@ -9,6 +9,8 @@ Solution for problem 23 of Project Euler https://projecteuler.net/problem=23 """ +from itertools import product + from utils import timeit @@ -34,25 +36,27 @@ def compute(): Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. """ - LIMIT = 28124 - divisorsum = [0] * LIMIT - for i in range(1, LIMIT): - for j in range(i * 2, LIMIT, i): - divisorsum[j] += i - abundantnums = [i for (i, x) in enumerate(divisorsum) if x > i] - expressible = [False] * LIMIT - for i in abundantnums: - for j in abundantnums: - if i + j < LIMIT: + limit = 28124 + divisor_sum = [0] * limit + for i in range(1, limit): + for j in range(i * 2, limit, i): + divisor_sum[j] += i + + abundant_nums = [i for (i, x) in enumerate(divisor_sum) if x > i] + + expressible = [False] * limit + for i in abundant_nums: + for j in abundant_nums: + if i + j < limit: expressible[i + j] = True else: break ans = sum(i for (i, x) in enumerate(expressible) if not x) + return ans if __name__ == "__main__": - - print(f"Result for Problem 23: {compute()}") \ No newline at end of file + print(f"Result for Problem 23 is {compute()}")