請問為什麼在cmd測試時秒數都壓在500ms內(單筆測資)
而送出解答卻變成TLE(9s)
請問有甚麼方法可以再縮短時間?
我的程式碼:
import sys
import time
for line in sys.stdin:
start = time.time()
a , b = map(int,line.split())
if a % 2 == 0:
a += 1
count = 0
if a == 1 or a ==2:
a = 3
count += 1
prime = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
for i in range(a,b+1,2):
if i in prime:
count += 1
continue
x = 1
for j in prime:
if i % j == 0:
x = 0
break
if x == 1:
if i > 10201:
for k in range(101,int(pow(i,0.5))+1,2):
if i % k == 0:
x = 0
break
count += 1
print(count)
end = time.time()
print(end-start)