From 2df02a8204a450e53e5d5dd04808910624075b7e Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 6 Apr 2026 15:49:51 +0200 Subject: [PATCH] Refactor problem --- .../problems_001_050/Problem012.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/project_euler_python/problems_001_050/Problem012.py b/src/project_euler_python/problems_001_050/Problem012.py index 1d17765..c453b41 100644 --- a/src/project_euler_python/problems_001_050/Problem012.py +++ b/src/project_euler_python/problems_001_050/Problem012.py @@ -9,7 +9,7 @@ Solution for problem 012 of Project Euler https://projecteuler.net/problem=12 """ -from itertools import count +from itertools import accumulate, count from math import floor, sqrt from project_euler_python.utils import timeit @@ -53,12 +53,10 @@ def compute(): divisors? """ - triangle = 0 - for i in count(1): - # This is the ith triangle number, i.e. num = 1 + 2 + ... + i = - # = i*(i+1)/2 - triangle += i - if num_divisors(triangle) > 500: + limit = 500 + for triangle in accumulate(count(1)): + # This is the ith triangle number, i.e. num = 1 + 2 + ... + i = i*(i+1)/2 + if num_divisors(triangle) > limit: return str(triangle)