-
Notifications
You must be signed in to change notification settings - Fork 0
/
5kyuConsecutivek-Primes.py
57 lines (46 loc) · 1.14 KB
/
5kyuConsecutivek-Primes.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
import math
def consec_kprimes(k, arr):
count = 0
found = False
for num in arr:
print(factors(num))
if len(factors(num)) == k:
if found:
count += 1
else:
found = True
else:
found = False
return count
def factors(num):
if num < 2:
return []
if is_prime(num):
return [num]
prime_factors = []
last_prime = 2
while last_prime <= math.floor(math.sqrt(num)):
if num % last_prime == 0:
prime_factors.append(last_prime)
num = num // last_prime
else:
last_prime += 1
while not is_prime(last_prime):
last_prime += 1
prime_factors.append(num)
return prime_factors
def is_prime(num):
if num < 2:
return False
if num % 2 == 0:
return num == 2
if num % 3 == 0:
return num == 3
if num < 9:
return True
for e in range(5, math.floor(math.sqrt(num))+1, 2):
if num % e == 0:
return False
return True
arr = [100489]
print(consec_kprimes(2, arr))