问题
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.
回答1:
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.
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.
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.
(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).
来源:https://stackoverflow.com/questions/24473055/relation-between-a-number-and-position-of-smallest-prime-larger-than-its-square