Solution to problem 47

This commit is contained in:
David Doblas Jiménez 2021-09-17 21:43:50 +02:00
parent abde258afa
commit fb87f1c9e3

60
src/Python/Problem047.py Normal file
View File

@ -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()}")