[PYTHON] Project Euler 35

problem

197 is called a cyclic prime number, because the numbers 197, 971, 719 obtained by rotating the digits are all prime numbers.

Less than 100 have 13 cyclic primes: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many traveling primes are less than 1 million? http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2035

Answer

0,2,4,5,6,8 are not included in the digits of cyclic prime numbers with two or more digits. This is because every time these numbers are in the first digit, they are divisible by 2 or 5. Therefore, other than 2,5, we should consider numbers that include only 1,3,7,9. In order to create this number of sets, I created a function called mapall. mapall is a function that combines each element e1 and e2 of a set iter1 for the number of times num specified by an appropriate associative law f and returns the result as a set type. First, using ['', 1,3,7,9] as iter1, I made a 6-digit number that is considered to be the largest. '' Is included so that when combined, the elements of the original list will remain in the combined list. For example,'' + '3' becomes '3', and the elements of the original list will remain in the combined list.

import mymath
def circle(num):
  s = str(num)
  for c in range(len(s)):
    s = s[1:]+s[0]
    yield int(s)

def mapall(f, iter1, num=1):
  ret = iter1
  for i in range(num):
    ret = set([f(e1,e2) for e1 in ret for e2 in iter1])
  return ret
  
def connect_num(e1,e2):
  return str(e1)+str(e2)

def main():
  pri = mymath.get_primes(10**6)
  n_list = ['',1,3,7,9]
  pn5 = mapall(connect_num, n_list,5)
  pn5.remove('')
  ans = {}
  for pn in pn5:
    if pn in ans:
      continue
    flag = True
    for n in circle(pn):
      if not pri['bool'][n]:
        flag = False
        break
    if flag:
      for n in circle(pn):
        ans[n] = True
  print len(ans)+2
main()

Recommended Posts

Project Euler 37
Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 17
Project Euler 26
Project Euler 23
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25
[Project Euler] problem1
Project Euler15 "Lattice Path"
Project Euler 2 Acceleration 2.21 Save microseconds.
Project Euler Original Method Group 1
What is Project Euler 3 Acceleration?
Functional programming in Python Project Euler 1
Project Euler 10 "Sum of Prime Numbers"
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Project Euler # 5 "Minimum Multiples" in Python
Project Euler 4 Attempt to speed up
Functional programming in Python Project Euler 2
Project Euler 11 "Maximum product in grid"
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler 9 Retention of calculation results
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
I wrote Project Euler 1 in one liner.
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python