-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem=47.py
More file actions
42 lines (36 loc) · 1.23 KB
/
problem=47.py
File metadata and controls
42 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Distinct Primes Factors - https://projecteuler.net/problem=47
def prime_sieve(limit):
sieve = [True] * (limit + 1)
sieve[0] = sieve[1] = False
for start in range(2, int(limit**0.5) + 1):
if sieve[start]:
for multiple in range(start*start, limit + 1, start):
sieve[multiple] = False
return [num for num, is_prime in enumerate(sieve) if is_prime]
def distinct_prime_factors_count(n, primes):
count = 0
for prime in primes:
if prime * prime > n:
break
if n % prime == 0:
count += 1
while n % prime == 0:
n //= prime
if n > 1:
count += 1
return count
def find_first_sequence(length, distinct_count):
limit = 1000000
primes = prime_sieve(limit)
consecutive = 0
for i in range(2, limit):
if distinct_prime_factors_count(i, primes) == distinct_count:
consecutive += 1
if consecutive == length:
return i - length + 1
else:
consecutive = 0
return None
if __name__ == "__main__":
result = find_first_sequence(4, 4)
print(f"The first four consecutive integers to have four distinct prime factors each start at: {result}")