Q35

# coding=UTF-8
print "Q34:\n"

'''
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

'''

#范围[1, 100-0000]

'''
算法
1.首先生成一百万内的素数
   使用map存一百万内的素数,key=素数值,value:0(重复),1(非重复)

2.for prime in map
      list_prime_rotation = get_list_rotation(prime)
      for x in list_prime_rotation
          if x not in map
            delete x from map
            found = false
      if found == false
          delete prime from map
'''

import tools
'''
ratation算法
1,2/2,3  917 971
2,3/3,1  179 917
3,1/1,2  719 179

rotate(list,pos1,pos2)
    x = list[pos1]
    list[pos1] = list[pos2]
    list[pos2] = x

index from 1 to len - 1
    step from 1 to len - 1
        rotate(index%(len+1),(index+1)%(len+1)+1)
        index = index + 1
        if index == len + 1
            index = 1
'''

def get_list_rotation(number):
    list_res = []
    list_digits = tools.int_to_list_digits_simple_but_not_fast(number)
    list_container = list_digits[:]
    l = len(list_digits)
    for i in range(0,l-1):
        j = i
        for step in range(0,l-1):
            pos1 = j%l
            pos2 = (j+1)%l
            x = list_digits[pos1]
            list_digits[pos1] = list_digits[pos2]
            list_digits[pos2] = x
            j = j + 1
            if j == l:
                j = 0
        list_res.append(int(''.join(map(str,list_digits))))
    return list_res

print get_list_rotation(197)
#print get_list_rotation(19)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值