目的
对素数进行range循环,赋值x,y,计算点与点到原点的距离
问题
从range循环中计算欧氏距离,移动数为质数,递增运行,使用j将移动数[j]分配给x,然后在每隔一个range循环中,将接下来的j个移动数[j +1]分配给y。序列应为x = 2 y = 3 x = 5 y = 7 x = 11 y = 13 x = 17 y = 19 x = 23 y = 29 x = 31 y = 37...x = 89 y = 97。然后每对x、y应该计算欧几里德距离。
编号
euclidean <- function(x, y) sqrt(sum((x - y)^2))
x = 0
y = 0
s = (NULL)
for (j in range(length(moves)-1)) {
if ((j %% 2) == 0) {
x <- moves[j]
} else {
y <- moves[j+1]
}
append(s, euclidean(x,y))
print(euclidean(x,y)) # just a test!
j=+1
}
output:
[1] 86
[1] 86
质数的数据
moves
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
1条答案
按热度按时间ifmq2ha21#
这里有一个办法。
创建
moves
的偶数索引向量。即使计算是成对进行的,也会有效地丢弃moves
的最后一个元素,即第25个元素,因为后面没有y
。然后,在sapply
循环中对每对元素调用euclidean
,一行程序将完成此操作。创建于2023年1月7日,使用reprex v2.0.2