在 Rstudio(使用 R 3.1.1)中运行时,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220
在 R 3.1.1 中,当我运行它时,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183
(正确的输出是 9183)
我不明白为什么......非常感谢帮助
最佳答案
正如 David Arenburg 所指出的,这是 32 位和 64 位 R 版本之间的区别,至少在 Windows 机器上是这样。据推测,涉及某种舍入误差。有趣的是,32 位 R 得到了正确的答案,而 64 位 R 找到了太多的唯一数字。
先确认一下9183
确实是正确答案,我用了gmp
包(C 多精度算术库 GMP 的包装器),它提供不受舍入误差影响的结果:
library(gmp)
x <- as.bigz(2:100)
length(unique(do.call(c, sapply(x, function(X) x^X))))
[1] 9183
这是我的 32 位 R 的结果:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9183
R.version[1:7] _
# platform i386-w64-mingw32
# arch i386
# os mingw32
# system i386, mingw32
# status
# major 3
# minor 1.2
这是我的 64 位 R 的结果:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9220
R.version[1:7]
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 3
# minor 1.2
关于Rstudio 和 R 终端给出不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258084/