It's my first program in Dart, and I just wanted to see it's asynchronous capabilities. Knowing javascript I wrote the following code:
import 'dart:async' show Timer;
import 'dart:math';
void main() {
//Recursion
fib1(x) => x > 1 ? fib1(x-1) + fib1(x-2) : x;
//Mathematical
num fi = (1 + sqrt(5)) / 2;
fib2(x) => x > 1 ? ((pow(fi, x) + pow(1 - fi, x)) / sqrt(5)).round() : x;
//Linear
fib3(x) {
if(x < 2) return x;
int a1 = 0;
int a2 = 1;
int sum = 0;
for(int i = 1; i < x; i++) {
sum = a2 + a1;
a1 = a2;
a2 = sum;
}
return sum;
}
Timer.run(() => print('Fib1:' + fib1(41).toString()));
Timer.run(() => print('Fib2:' + fib2(41).toString()));
Timer.run(() => print('Fib3:' + fib3(41).toString()));
}
and the output on the dart editor is:
Fib1:165580141
Fib2:165580141
Fib3:165580141
All 3 outputs are printed at the same time. Isn't that wrong? fib3 is much faster and should be printed first.