【发布时间】:2017-10-16 19:58:04
【问题描述】:
好的,所以我在这个函数的末尾有一个 for 循环,它应该输出在函数早期创建的生成的素数。
当我逐行运行代码时,一切正常,两个 for 循环将列表中的每个元素输出:PrimeList_p、PrimeList_q,到控制台窗口。
但是,当我通过调用函数运行代码时,for 循环仅将前 20 个左右的元素输出到控制台。为什么不打印整个列表?
require(gmp)
GenPrimes <- function(InitialSize) {
#List initialisation
PrimeList_p <<- list()
PrimeList_q <<- list()
#Loop initialisation
x <- 1
#LOOP START
while (x < 81) {
#Generate and compile prime numbers into Prime_List1.
PrimeList_p[[x]] <- nextprime(urand.bigz(size = InitialSize + x, seed =
Sys.time()))
x <- x+1
PrimeList_q[[x]] <- nextprime(urand.bigz(size = InitialSize + x, seed =
Sys.time()))
x <- x+1
}
#LOOP END
#Remove NULL entries in lists
PrimeList_p <<- PrimeList_p[-which(sapply(PrimeList_p, is.null))]
PrimeList_q <<- PrimeList_q[-which(sapply(PrimeList_q, is.null))]
cat("Prime p:")
for (i in 1:40){
message(PrimeList_p[[i]])
}
cat("Prime q")
for (j in 1:40){
message(PrimeList_q[[j]])
}
}
GenPrimes(1)
【问题讨论】:
标签: r list function loops console