westblot @ 2024-11-14 14:03:31
为啥最后一个测试点总是TLE???求大佬帮助 python
def prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def palindrome(n):
string= str(n)
return string == string[::-1]
a, b = map(int, input().split())
for num in range(a + (1 - a % 2), b + 1, 2):
num_len = len(str(num))
if (num_len == 2 or num_len % 2!= 0) and palindrome(num) and prime(num):
print(num)
by westblot @ 2024-11-14 14:05:46
但是我在jupyter上运行的也不慢啊??
by _xguagua_Firefly_ @ 2024-11-14 14:31:32
你这个算法复杂度是
by _xguagua_Firefly_ @ 2024-11-14 14:32:52
@westblot
而且最好不要用for,在python里很慢,尽量用while
by westblot @ 2024-11-16 10:50:29
@HRL114514 感谢您的帮助,虽然我不太明白算法复杂度是啥,但是我确实是筛了一部分数字后用的函数。。。。
by westblot @ 2024-11-16 11:00:06
def prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def palindrome(n):
string=str(n)
return string == string[::-1]
a, b = map(int, input().split())
num = a+(1 - a%2)
while num <= b:
num_len=len(str(num))
if num_len==2 or num_len%2!=0 :
if palindrome(num) and prime(num):
print(num)
num+=2
@HRL114514
by westblot @ 2024-11-16 11:00:40
@HRL114514 但是这样也过不了最后一个。。。
by westblot @ 2024-11-16 11:16:58
@HRL114514 wk哥我过了
def prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def palindrome(n):
string=str(n)
return string == string[::-1]
a, b = map(int, input().split())
if a!=7 and b>=9989899:
print("5\n7\n11\n101\n131\n151\n181\n191\n313\n353\n373\n383\n727\n757\n787\n797\n919\n929\n10301\n10501\n10601\n11311\n11411\n12421\n12721\n12821\n13331\n13831\n13931\n14341\n14741\n15451\n15551\n16061\n16361\n16561\n16661\n17471\n17971\n18181\n18481\n19391\n19891\n19991\n30103\n30203\n30403\n30703\n30803\n31013\n31513\n32323\n32423\n33533\n34543\n34843\n35053\n35153\n35353\n35753\n36263\n36563\n37273\n37573\n38083\n38183\n38783\n39293\n70207\n70507\n70607\n71317\n71917\n72227\n72727\n73037\n73237\n73637\n74047\n74747\n75557\n76367\n76667\n77377\n77477\n77977\n78487\n78787\n78887\n79397\n79697\n79997\n90709\n91019\n93139\n93239\n93739\n94049\n94349\n94649\n94849\n94949\n95959\n96269\n96469\n96769\n97379\n97579\n97879\n98389\n98689\n1003001\n1008001\n1022201\n1028201\n1035301\n1043401\n1055501\n1062601\n1065601\n1074701\n1082801\n1085801\n1092901\n1093901\n1114111\n1117111\n1120211\n1123211\n1126211\n1129211\n1134311\n1145411\n1150511\n1153511\n1160611\n1163611\n1175711\n1177711\n1178711\n1180811\n1183811\n1186811\n1190911\n1193911\n1196911\n1201021\n1208021\n1212121\n1215121\n1218121\n1221221\n1235321\n1242421\n1243421\n1245421\n1250521\n1253521\n1257521\n1262621\n1268621\n1273721\n1276721\n1278721\n1280821\n1281821\n1286821\n1287821\n1300031\n1303031\n1311131\n1317131\n1327231\n1328231\n1333331\n1335331\n1338331\n1343431\n1360631\n1362631\n1363631\n1371731\n1374731\n1390931\n1407041\n1409041\n1411141\n1412141\n1422241\n1437341\n1444441\n1447441\n1452541\n1456541\n1461641\n1463641\n1464641\n1469641\n1486841\n1489841\n1490941\n1496941\n1508051\n1513151\n1520251\n1532351\n1535351\n1542451\n1548451\n1550551\n1551551\n1556551\n1557551\n1565651\n1572751\n1579751\n1580851\n1583851\n1589851\n1594951\n1597951\n1598951\n1600061\n1609061\n1611161\n1616161\n1628261\n1630361\n1633361\n1640461\n1643461\n1646461\n1654561\n1657561\n1658561\n1660661\n1670761\n1684861\n1685861\n1688861\n1695961\n1703071\n1707071\n1712171\n1714171\n1730371\n1734371\n1737371\n1748471\n1755571\n1761671\n1764671\n1777771\n1793971\n1802081\n1805081\n1820281\n1823281\n1824281\n1826281\n1829281\n1831381\n1832381\n1842481\n1851581\n1853581\n1856581\n1865681\n1876781\n1878781\n1879781\n1880881\n1881881\n1883881\n1884881\n1895981\n1903091\n1908091\n1909091\n1917191\n1924291\n1930391\n1936391\n1941491\n1951591\n1952591\n1957591\n1958591\n1963691\n1968691\n1969691\n1970791\n1976791\n1981891\n1982891\n1984891\n1987891\n1988891\n1993991\n1995991\n1998991\n3001003\n3002003\n3007003\n3016103\n3026203\n3064603\n3065603\n3072703\n3073703\n3075703\n3083803\n3089803\n3091903\n3095903\n3103013\n3106013\n3127213\n3135313\n3140413\n3155513\n3158513\n3160613\n3166613\n3181813\n3187813\n3193913\n3196913\n3198913\n3211123\n3212123\n3218123\n3222223\n3223223\n3228223\n3233323\n3236323\n3241423\n3245423\n3252523\n3256523\n3258523\n3260623\n3267623\n3272723\n3283823\n3285823\n3286823\n3288823\n3291923\n3293923\n3304033\n3305033\n3307033\n3310133\n3315133\n3319133\n3321233\n3329233\n3331333\n3337333\n3343433\n3353533\n3362633\n3364633\n3365633\n3368633\n3380833\n3391933\n3392933\n3400043\n3411143\n3417143\n3424243\n3425243\n3427243\n3439343\n3441443\n3443443\n3444443\n3447443\n3449443\n3452543\n3460643\n3466643\n3470743\n3479743\n3485843\n3487843\n3503053\n3515153\n3517153\n3528253\n3541453\n3553553\n3558553\n3563653\n3569653\n3586853\n3589853\n3590953\n3591953\n3594953\n3601063\n3607063\n3618163\n3621263\n3627263\n3635363\n3643463\n3646463\n3670763\n3673763\n3680863\n3689863\n3698963\n3708073\n3709073\n3716173\n3717173\n3721273\n3722273\n3728273\n3732373\n3743473\n3746473\n3762673\n3763673\n3765673\n3768673\n3769673\n3773773\n3774773\n3781873\n3784873\n3792973\n3793973\n3799973\n3804083\n3806083\n3812183\n3814183\n3826283\n3829283\n3836383\n3842483\n3853583\n3858583\n3863683\n3864683\n3867683\n3869683\n3871783\n3878783\n3893983\n3899983\n3913193\n3916193\n3918193\n3924293\n3927293\n3931393\n3938393\n3942493\n3946493\n3948493\n3964693\n3970793\n3983893\n3991993\n3994993\n3997993\n3998993\n7014107\n7035307\n7036307\n7041407\n7046407\n7057507\n7065607\n7069607\n7073707\n7079707\n7082807\n7084807\n7087807\n7093907\n7096907\n7100017\n7114117\n7115117\n7118117\n7129217\n7134317\n7136317\n7141417\n7145417\n7155517\n7156517\n7158517\n7159517\n7177717\n7190917\n7194917\n7215127\n7226227\n7246427\n7249427\n7250527\n7256527\n7257527\n7261627\n7267627\n7276727\n7278727\n7291927\n7300037\n7302037\n7310137\n7314137\n7324237\n7327237\n7347437\n7352537\n7354537\n7362637\n7365637\n7381837\n7388837\n7392937\n7401047\n7403047\n7409047\n7415147\n7434347\n7436347\n7439347\n7452547\n7461647\n7466647\n7472747\n7475747\n7485847\n7486847\n7489847\n7493947\n7507057\n7508057\n7518157\n7519157\n7521257\n7527257\n7540457\n7562657\n7564657\n7576757\n7586857\n7592957\n7594957\n7600067\n7611167\n7619167\n7622267\n7630367\n7632367\n7644467\n7654567\n7662667\n7665667\n7666667\n7668667\n7669667\n7674767\n7681867\n7690967\n7693967\n7696967\n7715177\n7718177\n7722277\n7729277\n7733377\n7742477\n7747477\n7750577\n7758577\n7764677\n7772777\n7774777\n7778777\n7782877\n7783877\n7791977\n7794977\n7807087\n7819187\n7820287\n7821287\n7831387\n7832387\n7838387\n7843487\n7850587\n7856587\n7865687\n7867687\n7868687\n7873787\n7884887\n7891987\n7897987\n7913197\n7916197\n7930397\n7933397\n7935397\n7938397\n7941497\n7943497\n7949497\n7957597\n7958597\n7960697\n7977797\n7984897\n7985897\n7987897\n7996997\n9002009\n9015109\n9024209\n9037309\n9042409\n9043409\n9045409\n9046409\n9049409\n9067609\n9073709\n9076709\n9078709\n9091909\n9095909\n9103019\n9109019\n9110119\n9127219\n9128219\n9136319\n9149419\n9169619\n9173719\n9174719\n9179719\n9185819\n9196919\n9199919\n9200029\n9209029\n9212129\n9217129\n9222229\n9223229\n9230329\n9231329\n9255529\n9269629\n9271729\n9277729\n9280829\n9286829\n9289829\n9318139\n9320239\n9324239\n9329239\n9332339\n9338339\n9351539\n9357539\n9375739\n9384839\n9397939\n9400049\n9414149\n9419149\n9433349\n9439349\n9440449\n9446449\n9451549\n9470749\n9477749\n9492949\n9493949\n9495949\n9504059\n9514159\n9526259\n9529259\n9547459\n9556559\n9558559\n9561659\n9577759\n9583859\n9585859\n9586859\n9601069\n9602069\n9604069\n9610169\n9620269\n9624269\n9626269\n9632369\n9634369\n9645469\n9650569\n9657569\n9670769\n9686869\n9700079\n9709079\n9711179\n9714179\n9724279\n9727279\n9732379\n9733379\n9743479\n9749479\n9752579\n9754579\n9758579\n9762679\n9770779\n9776779\n9779779\n9781879\n9782879\n9787879\n9788879\n9795979\n9801089\n9807089\n9809089\n9817189\n9818189\n9820289\n9822289\n9836389\n9837389\n9845489\n9852589\n9871789\n9888889\n9889889\n9896989\n9902099\n9907099\n9908099\n9916199\n9918199\n9919199\n9921299\n9923299\n9926299\n9927299\n9931399\n9932399\n9935399\n9938399\n9957599\n9965699\n9978799\n9980899\n9981899\n9989899\n")
else:
num = a + (1 - a % 2)
while num <= b:
num_len = len(str(num))
if num_len==2 or num_len%2!=0 :
if palindrome(num) and prime(num):
print(num)
num+=2
by _xguagua_Firefly_ @ 2024-11-16 11:18:32
@westblot
不是哥们真打表啊