-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem-049.py
77 lines (67 loc) · 2.12 KB
/
problem-049.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
### Problem 49 - Prime Permutations
###------------------------------------------------------------------------------------------------------------------------------------------------------------
### The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways:
### (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
### There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
### What 12-digit number do you form by concatenating the three terms in this sequence?
### Solution
# Function to determine if prime
def isPrime(n: int) -> bool:
if n < 2:
return False
elif n == 2:
return True
else:
i = 2
while i ** 2 <= n:
if n % i == 0:
return False
i += 1
return True
# Function to determine if permutation
def isPerm(a: int, b: int) -> bool:
digits = list()
for i in str(a):
digits.append(i)
for j in str(b):
if j in digits:
digits.remove(j)
return len(digits) == 0
# Generate list of 4-digit primes
primes = []
n = 1000
while True:
if n > 9999:
break
if isPrime(n):
primes.append(n)
n += 1
# Generate list of permutations of 4-digit primes
perms = []
for i in range(len(primes)):
currPerms = [primes[i]]
for j in range(i, len(primes)):
if primes[i] == primes[j]:
continue
elif isPerm(primes[i], primes[j]):
currPerms.append(primes[j])
if len(currPerms) > 2:
perms.append(currPerms)
# Determine if there is a constant difference in sequence
sequences = []
for i in perms:
diff = i[1] - i[0]
const = True
for j in range(1, len(i) - 1):
if i[j + 1] - i[j] != diff:
const = False
break
if const == True:
sequences.append(i)
finalNum = ""
for i in sequences:
finalNum += str(i)
print(
"The 12-digit number from concatenating the number sequence is: "
+ finalNum
)