Replies: 1 comment 4 replies
-
同学,我建议你先用markdown格式吧缩进体现出来先,我们无法阅读你的代码
…________________________________
From: sjdl233 ***@***.***>
Sent: Tuesday, October 24, 2023 4:53:57 PM
To: datawhalechina/learn-python-the-smart-way-v2 ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [datawhalechina/learn-python-the-smart-way-v2] 代码运行过慢问题 (Discussion #27)
import ast
n = ast.literal_eval(input())
def hw(n): #回文数
a=0
while n>a:
a=a*10+n%10
n//=10
if n==a or n==a//10:
return True
else:
return False
def zs(num): #质数
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def hz(n):
i=0
c=-1
while True:
i=i + 1
if i>11 and i<101:
i=101
if hw(i) and zs(i):
c=c + 1
if c==n:
return i
print(hz(n))
这段代码在n=5以上运行速度很慢,我尝试过将hw代码用字符串方式撰写,速度会快,请问这是为什么
―
Reply to this email directly, view it on GitHub<#27>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AHFTSDPE66QFPAYAXWAZAB3YA562LAVCNFSM6AAAAAA6NKZX36VHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZVG43TCNJWHE>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
这段代码在n=5以上运行速度很慢,我尝试过将hw代码用字符串方式撰写,速度会快,请问这是为什么
Beta Was this translation helpful? Give feedback.
All reactions