在素数的列表中的数量和位置最小质比其平方根较大的关系 [英] Relation between a number and position of smallest prime larger than its square root in the list of primes

查看:156
本文介绍了在素数的列表中的数量和位置最小质比其平方根较大的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时使素数的列表,需要检测一个数是否整除任何素等于或大于它的平方根小。而不是检查每次一个素数是否大于数的平方根被检查(为已完成当前页上但:<一href="http://stackoverflow.com/questions/24467936/optimize-prime-number-finder-and-cpu-speed-checker-in-r/24468241#comment37872163_24468241">Optimize素数取景器,并在研发 CPU速度检查),这将是有用的,如果我们可以知道其在列表中的位置。是否有一个数和最小质比其平方根素数的列表较大的位置之间的任何关系或方程。以下code(在R)和图表明,有可能是一个关系:

While making a list of prime numbers, one needs to check if a number is divisible by any prime equal to or smaller than its square root. However, instead of checking every time whether a prime number is more than square root of number being checked (as done on this page: Optimize prime number finder and cpu speed checker in R ), it will be useful if one can know its position in the list. Is there any relation or equation between a number and the position of smallest prime larger than its square root in a list of prime numbers. Following code (in R) and graph shows that there may be a relation:

pnum_sqrtpos_plot = function(){
    p_nums = c(2L,3L)
    counter = 3L
    sqrtcounter=1L
    sqrtposlist = c(1L,1L)
    countchecker=10000
    while (counter<200000){
        isPRIME = FALSE
        counter = counter +2L
        sqrtcounter = sqrt(counter)
        for(n in p_nums) {
            if(n > sqrtcounter){ isPRIME=TRUE; break; }
            if(counter %% n == 0){ isPRIME = FALSE; break;}
        }
        if(isPRIME) { p_nums[length(p_nums)+1]=counter 
            sqrtpos = which(p_nums>sqrt(counter))[1]
            sqrtposlist[length(sqrtposlist)+1] = sqrtpos
        }
        if(counter > countchecker) {cat("Numbers checked: ",counter,"\n");
                counterchecker = countchecker+10000;
        }
    }
    plot(p_nums, sqrtposlist
    , main="Relation between number & \nposition of smallest prime larger than its square root in list of primes"
    , xlab="Number", ylab="Position in list of prime numbers")
}

可能是什么关系/方程式?感谢您的帮助。

What could be the relation / equation? Thanks for your help.

推荐答案

在你点,你正在建立按升序排列,在每个点素数的列表,例如:code您检查数素性在列表测试整除每个数最多的要数被测试的平方根。

In the example code you point to, you are building up a list of prime numbers in ascending order and at each point you check a number for primality by testing divisibility by each number in the list up to the square root of the number being tested.

如果您在按升序排列长度的号码清单n您可以搜索该列表使用二进制印章,这比搜索更快找到比的sqrt(目标)的时间对数列表的长度,数量最多的小通过列表中的一个在一个时间

If you have a list of numbers in ascending order of length n you can search this list to find the largest number smaller than sqrt(target) in time logarithmic in the length of the list using binary chop, which is faster than searching through the list one at a time.

事实上,你可以做得比这更好,因为你正在寻找升序新的素数。如果你还记得人数最多的超过的sqrt(目标)为previous数量较小的位置,你可以期待从这里时,下一个数字打交道搜索 - 让每一个号码,你通常只需要检查你不'吨需要改变最大的考验除数检查,并有一个位置进行移动时的一小部分。

In fact you can do better than this because you are looking for new primes in ascending order. If you remember the position of the largest number smaller than sqrt(target) for the previous number, you can search forward from here when dealing with the next number - so for each number you usually only have to check that you don't need to change the largest test divisor to check, and a small proportion of the time you have to move it by one position.

(这些都是有趣的小调整,但我怀疑他们是否会加快您的code。通过任何明显的量我注意到,有一个在探查的 http://stat.ethz.ch/R-manual/R-devel/library/utils/html/ Rprof.html ,你可以用它来看看你ř程序花费的时间,当优化code这通常是值得在运行这样的剖析,因为人类是非常糟糕的猜测什么是昂贵的,什么是没有了,没有点倍增code表示仅消耗的总时间的1%)。

(These are all interesting little tweaks, but I doubt if they will speed up your code by any noticeable amount. I note that there is a profiler at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rprof.html which you can use to see where your R program is spending its time. When optimising code it is usually worth while running such profilers, because humans are very bad at guessing what is expensive and what is not, and there is no point doubling the speed of code that is only consuming 1% of the total time).

这篇关于在素数的列表中的数量和位置最小质比其平方根较大的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆