-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_p47.py
57 lines (50 loc) · 1.47 KB
/
euler_p47.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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² × 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?
"""
from math import sqrt
def isPrime(num):
if num in [2, 3, 5, 7, 11, 13, 17, 19, 23]:
return True
elif num%2 == 0: return False
elif num%3 == 0: return False
else:
i = 5
k = 2
while i**2 <= num:
if num%i == 0: return False
i += k
k = 6 - k
return True
def countdivisors(num):
bignum = num
primedivisors = {}
i = 2
while i <= int(sqrt(bignum)):
if isPrime(i):
while bignum % i == 0:
if i not in primedivisors.keys(): primedivisors[i] = 0
primedivisors[i] += 1
bignum /= i
if isPrime(bignum):
if bignum not in primedivisors.keys(): primedivisors[bignum] = 0
primedivisors[bignum] += 1
break
if isPrime(bignum): break
else:
i += 1
else: i += 1
return len(primedivisors.keys())
num = 0
success = False
while not success:
num += 1
success = all([countdivisors(i)==4 for i in range(num, num+4)])
print(num)