'low..high' 的边界必须是兼容类型的整数

Bounds of 'low..high' must be integers of compatible types

我刚开始学习 chapel 并尝试了一个简单的埃拉托色尼筛法算法,但我无法将数组传递到过程中。

下面是实现

config const n:int = 5;
var arrOfErtosthenes : [1..n] bool;

arrOfErtosthenes = true;
proc sieveOfEratothenes(array,n){
    for i in 2..sqrt(n){
        if(array[i] == true){
            for j in i*i..n{
                array[j] = false;
            }
        }
    }
}

sieveOfEratothenes(arrOfErtosthenes,n);
for i in arrOfErtosthenes.domain do{
    if(arrOfErtosthenes[i] == true) then write(i," , ");
}
writeln();

在此之前,我尝试从用户那里获取 var n,但在这两种情况下都出现了以下错误

$ chpl /e/Programming/Chapel/simpleExcercises.chpl
/e/Programming/Chapel/simpleExcercises.chpl:9: In function 'sieveOfEratothenes':
/e/Programming/Chapel/simpleExcercises.chpl:10: error: Bounds of 'low..high' must be integers of compatible types.
  /e/Programming/Chapel/simpleExcercises.chpl:19: called as sieveOfEratothenes(array: [domain(1,int(64),false)] bool, n: int(64))
note: generic instantiations are underlined in the above callstack

错误来源在这一行:

    for i in 2..sqrt(n) {

范围 2..sqrt(n) 由两种不同的类型组成,intreal。要解决此问题,您可以将 sqrt(n) 转换为 int,如下所示:

    for i in 2..(sqrt(n):int) {