Refactor problem

This commit is contained in:
2026-04-06 15:49:51 +02:00
parent 8580160d67
commit 2df02a8204

View File

@@ -9,7 +9,7 @@ Solution for problem 012 of Project Euler
https://projecteuler.net/problem=12 https://projecteuler.net/problem=12
""" """
from itertools import count from itertools import accumulate, count
from math import floor, sqrt from math import floor, sqrt
from project_euler_python.utils import timeit from project_euler_python.utils import timeit
@@ -53,12 +53,10 @@ def compute():
divisors? divisors?
""" """
triangle = 0 limit = 500
for i in count(1): for triangle in accumulate(count(1)):
# This is the ith triangle number, i.e. num = 1 + 2 + ... + i = # This is the ith triangle number, i.e. num = 1 + 2 + ... + i = i*(i+1)/2
# = i*(i+1)/2 if num_divisors(triangle) > limit:
triangle += i
if num_divisors(triangle) > 500:
return str(triangle) return str(triangle)