在Rstudio中(使用R 3.1.1)运行时,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220
Run Code Online (Sandbox Code Playgroud)
在R 3.1.1中,当我运行时,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183
Run Code Online (Sandbox Code Playgroud)
(正确的输出是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
Run Code Online (Sandbox Code Playgroud)
以下是我的 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
Run Code Online (Sandbox Code Playgroud)
以下是我的 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
Run Code Online (Sandbox Code Playgroud)