5 回答
TA贡献1942条经验 获得超3个赞
Q1
n <- 1:30
f <- c(1,1)
for (i in n) f <- c(f, f[length(f)-1]+f[length(f)])
for (i in 1:(length(f)-1)) print(f[i]/f[i+1])
从结果可以看到,貌似收敛到 0.618
Q2
1) answer <- c(3)
在for循环里:
#每次都从answer中取最后一项,并把计算结果存到answer中(作为最后一项);
2) j =1时: answer <- c(answer, ( 7* answer[ 1 ] ) %% 31) ==> answer = c(3, 21)
3) j =2时:answer <- c(answer, ( 7* answer[ 2 ] ) %% 31) => answer = c(3,21, 21*7%%31)
--->23<-----
4) ...
16)j=15时: answer中有16个元素;
TA贡献1878条经验 获得超4个赞
updateR()
在其他目录下安装 R ,再将旧版本保留的 library 目录下的文件拷贝至新版本 library 目录下,然后update.packages() ;或卸载 R ,把 R 装到旧的目录下,然后 update.packages()。
TA贡献1796条经验 获得超10个赞
f[1]<-f[2]<-1 和 我的f<-c(1,1) 是等价的 ,仅仅是风格不同而已; f[1] <- f[2] <- 1 可以大致表示成: 1) f <- c(0, 1) 2) f[1] <- 1 ==> f = c(1,1) f[i] <- f[i-2] + f[i-1] 的意思是第三项是第一项和第二项的和 ,和我的也是风格不同,你的代码更符合r的风格 r语言的数组可以增长,当下标超出当前的实际长度时,数组就增长到下标长度; 所以F[i] <- F[i-2] + F[i-1]的意思是:数组F长度增长1,增长的这项的取值是前两项的和; 我的代码把这个过程展开了,f <- c(f, f[length(f)-1]+f[length(f)])的意思是 f重新赋值,取值是这样一个数组,这个数组包含原来的f全部,并在最后追加一项,这一项的值是数组f的最后两项的和;其中length(f)是取数组f的最后一项的下标,length(f)-1是倒数第二项的下标;c(数组,元素)的意思是把用数组和元素构成一个新的数组,新数组的长度是旧数组的长度+1;
TA贡献1860条经验 获得超8个赞
#Q1 f = c(1,1) for(i in 2:30) { print(sprintf("i=%i, f[i-1]=%i, f[i]=%i, f[i-1]/f[i]=%.4f", i, f[i-1], f[i], f[i]/f[i-1])) f=c(f, (f[i-1]+f[i])) # sum of last two element in f } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # Q2. 每一步都打印出值,你可以参照一下 > for ( j in 1:15) { + print(sprintf("j=%i, answer[j]=%i, 7*answer[j]=%i, 7*answer[j]%%31=%i", j, answer[j], 7*answer[j], (7*answer[j])%%31)) + answer <- c(answer, ( 7* answer[ j ] ) %% 31) + } [1] "j=1, answer[j]=3, 7*answer[j]=21, 7*answer[j]%31=21" [1] "j=2, answer[j]=21, 7*answer[j]=147, 7*answer[j]%31=23" [1] "j=3, answer[j]=23, 7*answer[j]=161, 7*answer[j]%31=6" [1] "j=4, answer[j]=6, 7*answer[j]=42, 7*answer[j]%31=11" [1] "j=5, answer[j]=11, 7*answer[j]=77, 7*answer[j]%31=15" [1] "j=6, answer[j]=15, 7*answer[j]=105, 7*answer[j]%31=12" [1] "j=7, answer[j]=12, 7*answer[j]=84, 7*answer[j]%31=22" [1] "j=8, answer[j]=22, 7*answer[j]=154, 7*answer[j]%31=30" [1] "j=9, answer[j]=30, 7*answer[j]=210, 7*answer[j]%31=24" [1] "j=10, answer[j]=24, 7*answer[j]=168, 7*answer[j]%31=13" [1] "j=11, answer[j]=13, 7*answer[j]=91, 7*answer[j]%31=29" [1] "j=12, answer[j]=29, 7*answer[j]=203, 7*answer[j]%31=17" [1] "j=13, answer[j]=17, 7*answer[j]=119, 7*answer[j]%31=26" [1] "j=14, answer[j]=26, 7*answer[j]=182, 7*answer[j]%31=27" [1] "j=15, answer[j]=27, 7*answer[j]=189, 7*answer[j]%31=3" > |
- 5 回答
- 0 关注
- 967 浏览
添加回答
举报