|
发表于 2022-10-29 15:45:16
|
显示全部楼层
- import time as t
- import math
- start = t.perf_counter()
- def check_prime(a_num):
- is_prime = True
- for prime_i in range(2, int(math.sqrt(a_num) + 1)):
- if a_num % prime_i == 0:
- is_prime = False
- return is_prime
- def is_exchangeable(prime_list):
- length_list = len(prime_list)
- for each_prime1 in range(length_list - 1):
- for each_prime2 in range(each_prime1 + 1, length_list):
- new_num_1 = int(str(prime_list[each_prime1]) + str(prime_list[each_prime2]))
- new_num_2 = int(str(prime_list[each_prime2]) + str(prime_list[each_prime1]))
- if not check_prime(new_num_1) or not check_prime(new_num_2):
- return False
- return True
- def p60():
- primes = []
- prime_range = 10000
- for numbers in range(2, prime_range):
- if check_prime(numbers):
- primes.append(numbers)
- primes.remove(2)
- primes.remove(5)
- test_range = len(primes)
- for prime1 in range(test_range):
- for prime2 in range(prime1 + 1, test_range):
- cur_plist = [primes[prime1], primes[prime2]]
- if is_exchangeable(cur_plist):
- for prime3 in range(prime2 + 1, test_range):
- cur_plist = [primes[prime1], primes[prime2], primes[prime3]]
- if is_exchangeable(cur_plist):
- for prime4 in range(prime3 + 1, test_range):
- cur_plist = [primes[prime1], primes[prime2], primes[prime3], primes[prime4]]
- if is_exchangeable(cur_plist):
- for prime5 in range(prime4 + 1, test_range):
- cur_plist = [primes[prime1], primes[prime2], primes[prime3],
- primes[prime4], primes[prime5]]
- if is_exchangeable(cur_plist):
- return cur_plist
- answer = p60()
- print(answer)
- print(sum(answer))
- print("It costs %f s" % (t.perf_counter() - start))
复制代码
[13, 5197, 5701, 6733, 8389]
26033
It costs 51.725813 s |
|