From a036c30fc425a2584bfeef6211257a1393643680 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sat, 11 Apr 2026 15:35:30 +0200 Subject: [PATCH] Refactor without building the whole list --- src/project_euler_python/problems_001_050/Problem024.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/project_euler_python/problems_001_050/Problem024.py b/src/project_euler_python/problems_001_050/Problem024.py index 2645b9b..65e6de2 100644 --- a/src/project_euler_python/problems_001_050/Problem024.py +++ b/src/project_euler_python/problems_001_050/Problem024.py @@ -9,7 +9,7 @@ Solution for problem 024 of Project Euler https://projecteuler.net/problem=24 """ -from itertools import permutations +from itertools import islice, permutations from project_euler_python.utils import timeit @@ -28,9 +28,10 @@ def compute(): 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? """ + limit = 1_000_000 digits = list(range(10)) - _permutations = list(permutations(digits)) - ans = "".join(str(digit) for digit in _permutations[999_999]) + millionth_permutation = next(islice(permutations(digits), limit - 1, limit)) + ans = "".join(str(digit) for digit in millionth_permutation) return ans