-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path047.py
More file actions
57 lines (42 loc) · 1.36 KB
/
047.py
File metadata and controls
57 lines (42 loc) · 1.36 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Project Euler Problem 47
========================
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^2 * 7 * 23
645 = 3 * 5 * 43
646 = 2 * 17 * 19.
Find the first four consecutive integers to have four distinct primes
factors. What is the first of these numbers?
"""
from collections import deque
from util import PrimeFactory
def distinctive_factors(factors):
for i, factors1 in enumerate(factors):
for factors2 in [f for j, f in enumerate(factors) if j > i]:
for f in factors1:
if f in factors2:
return False
return True
def find_n_cons_nums(n):
"""Finds the first n consecutive numbers that have n disctintive prime
factors"""
primes = PrimeFactory()
# First number that can be decomposed is 4
i = 4
factors = deque([primes.get_prime_factors(f) for f in range(i, i+n)])
while sum([0 if len(f) == n else 1 for f in factors]) != 0 or \
not distinctive_factors(factors):
factors.rotate()
i += 1
factors[-1] = primes.get_prime_factors(i + n - 1)
return i
def main():
assert find_n_cons_nums(2) == 14
assert find_n_cons_nums(3) == 644
print(find_n_cons_nums(4))
if __name__ == '__main__':
main()