本文主要是介绍Python练习题答案: 圆素数【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
圆素数【难度:1级】:
答案1:
def circular_permutations(n):n = str(n)return [int(n[i:] + n[:i]) for i in range(len(n))]def is_prime(n):return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5)+1))def circular_prime(n):return all(is_prime(x) for x in circular_permutations(n))
答案2:
def circular_prime(n):return n in [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 197, 199, 311, 337, 373, 719, 733, 919, 971, 991, 1193, 1931, 3119, 3779, 7793, 7937, 9311, 9377, 11939]
答案3:
def is_prime(n):return n > 1 and all(n % i for i in xrange(2, int(n ** 0.5) + 1))def circular_prime(n):num = str(n) * 2length = len(num) / 2for a in xrange(length):if not is_prime(int(num[a:a + length])):return Falsereturn True
答案4:
from gmpy2 import is_primedef circular_prime(n):n=str(n)return all(is_prime(int(n[i:]+n[:i])) for i in range(len(n)))
答案5:
def circular_prime(n):ns = str(n)lp = []for i in ns:ns = ns[1:] + ns[0]lp.append(ns)for i in lp:for j in range(2,int(i)):if int(i)%j == 0:return Falsereturn True and n != 1
答案6:
from collections import dequedef is_prime(n):return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5) + 1))def circular_prime(n):items = deque(str(n))for _ in range(len(items)):items.rotate(1)if not is_prime(int("".join(items))):return Falsereturn True
答案7:
def circular_permutations(n):s = str(n)return [int(s[i:] + s[:i]) for i in range(len(s))]def is_prime(n):return n == 2 or pow(2, n - 1, n) == 1def circular_prime(n):return all(is_prime(c) for c in circular_permutations(n))
答案8:
def is_prime(n):d = 2while d * d <= n:if n % d == 0:return Falsed += 1return n > 1def rotate(l, n):return l[-n:] + l[:-n]def circular_prime(number):number_list = [int(x) for x in str(number)]if is_prime(number):check_list = [True]for index in range(1,len(number_list)):number_rotated = rotate(number_list,index)number_join = int(''.join(map(str, number_rotated)))if is_prime(number_join):check_list.append(True)if (len(check_list) == len(number_list)): return Trueelse: return Falseelse: return False
答案9:
def circular_prime(n):return chr(n + 0x4E00) in '丑冗凋僝七乏併乡粣仅助佑亃丂丅峃乇义像严洁仇凟不下丟娯喋汱犡伷牟万乱'
景越Python基础训练营QQ群
欢迎各位同学加群讨论,一起学习,共同成长!
这篇关于Python练习题答案: 圆素数【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!