-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path047_distinct_primes_factors.py
37 lines (29 loc) · 1 KB
/
047_distinct_primes_factors.py
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
'''Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?'''
from math import sqrt
def get_primes_in_range(upper):
def is_prime(n):
'''Doesnt check divisibility by 2, only works with the list comprehension underneath.'''
for div in range(3, int(sqrt(n)) + 1, 2):
if not n % div:
return False
return True
return [2] + [num for num in range(3, upper, 2) if is_prime(num)]
primes = get_primes_in_range(1000)
def get_prime_factors(num):
prime_factors = set()
for p in primes:
if num <= 1:
return len(prime_factors)
while not num % p:
num //= p
prime_factors.add(p)
def get_first_of_consecutive_nums():
streak = 0
for i in range(1_000_000):
if get_prime_factors(i) != 4:
streak = 0
continue
streak += 1
if streak == 4:
return i - 3
print(get_first_of_consecutive_nums())