From fb87f1c9e3a120a4bf69722decd922e48e785dfd Mon Sep 17 00:00:00 2001 From: daviddoji Date: Fri, 17 Sep 2021 21:43:50 +0200 Subject: [PATCH] Solution to problem 47 --- src/Python/Problem047.py | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Python/Problem047.py diff --git a/src/Python/Problem047.py b/src/Python/Problem047.py new file mode 100644 index 0000000..342abdb --- /dev/null +++ b/src/Python/Problem047.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +""" +Created on 12 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for problem 47 of Project Euler +https://projecteuler.net/problem=47 +""" + +from utils import timeit + +def factor(n): + ans = [] + d = 2 + while d * d <= n: + if n % d == 0: + ans.append(d) + n //= d + else: + d += 1 + if n > 1: + ans.append(n) + return ans + +@timeit("Problem 47") +def compute(): + """ + The first two consecutive numbers to have two distinct prime factors are: + + 14 = 2 × 7 + 15 = 3 × 5 + + The first three consecutive numbers to have three distinct prime factors are: + + 644 = 2² × 7 × 23 + 645 = 3 × 5 × 43 + 646 = 2 × 17 × 19. + + Find the first four consecutive integers to have four distinct prime factors each. + What is the first of these numbers? + """ + ans = [] + + for number in range(1, 1_000_000): + if len(ans) == 4: + break + elif len(set(factor(number))) == 4: + ans.append(number) + else: + ans = [] + + return ans[0] + + + +if __name__ == "__main__": + + print(f"Result for Problem 47: {compute()}") \ No newline at end of file