-
Notifications
You must be signed in to change notification settings - Fork 0
/
p049.py
40 lines (32 loc) · 1.81 KB
/
p049.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
################################################################################
# P49: Prime permutations #
################################################################################
# #
# The sequence 1487, 4817, 8147, contains three primes, each increasing by #
# 3330 and each a permuation of one another. There is one other 4-digit #
# increasing sequence. #
# #
# What 12-digit number do you form by concatenating the three terms in this #
# sequence? #
# #
################################################################################
# Problem found at projecteuler.net #
# Author: ncfgrill #
################################################################################
from math import ceil, sqrt
def check_prime(num):
if num % 6 != 1 and num % 6 != 5: return False
for i in range(2, ceil(sqrt(num)) + 1):
if num % i == 0: return False
return True
def find_perm():
for i in range(1001, 4001, 2):
if i == 1487 or not check_prime(i): continue
perms, check = [i], ''.join(sorted(str(i)))
while len(perms) != 3:
i += 3330
if check_prime(i):
if check != ''.join(sorted(str(i))): break
perms.append(i)
if len(perms) == 3: return ''.join(str(x) for x in perms)
print('Number:', find_perm())