-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49_prime_permutatations.py
52 lines (34 loc) · 1 KB
/
49_prime_permutatations.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
import math
already_found_number = 1487
def is_prime(n):
d = 3
while n // d >= math.sqrt(n):
if n % d == 0:
return False
d += 2
return True
def compute_perm_dict(number):
digits = {}
for i in str(number):
if digits.get(i):
digits[i] += 1
else:
digits[i] = 1
return digits
def is_perm(base, number):
digits = compute_perm_dict(number)
return digits == base
def unusual_sequence(starting_value):
perm_base = compute_perm_dict(starting_value)
for i in range(1, 3):
number = starting_value + i * 3330
if not is_prime(number) or not is_perm(perm_base, number):
return False
return True
def prime_permutations():
for i in range(1001, 3339, 2): # only take the odd numbers
if not is_prime(i) or i == already_found_number:
continue
if unusual_sequence(i):
return f'{i}{i + 3330}{i + 6660}'
print(prime_permutations()) # 296962999629