a. Based on the function check.prime, create a new function that can count the number
of primes smaller than the given natural number. Name this new function count.prime. check.prime是检测给出的X是不是质数,然后count.prime是计算有多少小于X的数是质数,各位,谁知道这个方程该怎么写
b. Let x=c(100*seq(1:500)). Apply the function count.prime to the whole vector of
x and save the counts (output) in y (should be a vector with the same length as x.
De¯ne z=x/log(x). Display y and z. y=count.prime(x);x=c(100*seq(100:500); z=x/log(x)
c. Plot y versus x. On the same same firguer, add a curve z versus x. Are these two curves
close?
check.prime的指令如下
check.prime<-function(x)
{
if (x<=0 | x%%1!=0)
{out=paste("The input must be a positve integer")}
else {
aa=paste("The input", x, "IS NOT a prime number.")
bb=paste("The input", x, "IS a prime number.")
if (x==1) out=aa
if (x==2 | x==3) out=bb
if (x>=4)
{
m=2; flag=0
while (m<=sqrt(x) & flag==0)
{
if (x%%m==0) flag=1
m=m+1
}
out=ifelse(flag==1, aa, bb)
}
}
out
}