gpt4 book ai didi

python - 查找给定范围内的特殊数字的数量

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:20 25 4
gpt4 key购买 nike

我无法降低这个问题的复杂性。请提供一些更好的方法。

有没有我不知道的数学公式,或者可以用更好的方法来完成?

Problem Link

描述:

 A special number is not divisible by any number of the form Z*Z where (Z>1).

问题:找出给定范围内特殊数字的个数。

整数限制:10^9

我是这样做的:

    import math
def special(x):
flag=1
i=2
if(x==0 or x==1 or x==2):
return 1
while(i*i <= x): //This is the best i can think to limit the numbers.
if(x%(i*i)==0):
flag=0
break
i=i+1
return flag

t=int(raw_input())
while(t):
x,y=map(int,raw_input().split())
count=0
for i in xrange(x,y+1):
if(special(i)):
count+=1
print(count)
t=t-1

最佳答案

special(x) 中,您只需迭代小于或等于 sqrt(x) 的素数。所以我会预先计算一个素数列表 ( Fastest way to list all primes below N )。

关于python - 查找给定范围内的特殊数字的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23094021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com