我想生成并打印前10个Fibonacci数字。我不想提高效率,但我希望看到一些易于理解的(工作)X10代码。
// file Fibonacci.x10
public class Fibonacci {
public static def fib(n:Int): Int {
if (n < 2) {
return n;
}
val f1:Int;
val f2:Int;
finish {
async f1 = fib(n-1);
async f2 = fib(n-2);
}
return f1 + f2;
}
public static def main(args:Rail[String]) {
x10.io.Console.OUT.println("This is fibonacci in X10.");
for (var i:Int=0; i < 10; ++i) {
x10.io.Console.OUT.println(i + ": " + fib(i));
fib(i);
}
}
}
当我编译它时,我得到:
/home/moose/Fibonacci.x10:11: No valid method call found for call in given type.
Call: fib(x10.lang.Long)
Type: Fibonacci
/home/moose/Fibonacci.x10:12: No valid method call found for call in given type.
Call: fib(x10.lang.Long)
Type: Fibonacci
/home/moose/Fibonacci.x10:19: Cannot assign expression to target; base types are incompatible.
Expression: 0L
Expected base type: x10.lang.Int
Found base type: x10.lang.Long
3 errors.
我使用X10版本2.4.2。
答案 0 :(得分:1)
以下版本按预期工作:
// file Fibonacci.x10
public class Fibonacci {
public static def fib(n:Long): Long {
if (n < 2) {
return n;
}
val f1:Long;
val f2:Long;
finish {
async f1 = fib(n-1);
async f2 = fib(n-2);
}
return f1 + f2;
}
public static def main(args:Rail[String]) {
x10.io.Console.OUT.println("This is fibonacci in X10.");
for (var i:Long=0; i < 10; ++i) {
x10.io.Console.OUT.println(i + ": " + fib(i));
}
}
}
似乎每个标准的数字为Long
。