楼主: elysiumking
1788 3

求助!!!!!(急切)关于R的题 [推广有奖]

  • 0关注
  • 0粉丝

小学生

78%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
50 点
帖子
6
精华
0
在线时间
6 小时
注册时间
2010-10-11
最后登录
2013-8-8

楼主
elysiumking 发表于 2010-10-12 00:23:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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
}


各位大师,知道怎么做的,来帮帮忙!!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:function smaller Natural Display Vector 求助

回帖推荐

alphastatist 发表于3楼  查看完整内容

x=c(100*seq(1:50)) tmp

alphastatist 发表于2楼  查看完整内容

count.prime

本帖被以下文库推荐

沙发
alphastatist 发表于 2010-10-13 00:40:25
count.prime<-function(n){
y<-1:(n-1)
z<-rep(FALSE,n-1)
for (i in y){
z<-check.prime(i)
}
return(sum(z))
}

仅供参考,可进一步优化

1# elysiumking
http://www.pinggu.org/bbs/images/logo2.gif[/IMG][/URL]

藤椅
alphastatist 发表于 2010-10-13 01:03:22
x=c(100*seq(1:50))
tmp<-as.matrix(x)
y<-apply(tmp,1,count.prime)
plot(x,y)
z<-x/log(x)
lines(x,z,col='BLUE')
注意x改成1:50了,500计算时间太长
http://www.pinggu.org/bbs/images/logo2.gif[/IMG][/URL]

板凳
elysiumking 发表于 2010-10-13 09:40:52
谢谢了,大师!!!!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-18 22:45