Dynamic Programming

Run Settings
LanguageDart
Language Version
Run Command
void main() { addTo80(n){ print('long time '); return n + 80; } memoizedAddTo80(){ var cache = {}; return (n){ if(cache.containsKey(n)){ return cache; }else{ cache[n] = n + 80; return cache; } } ; } var calculations = 0; fibonacci(n){ calculations++; if(n < 2){ return n; } return fibonacci(n-1) + fibonacci(n-2); } fibonacciMaster(){ var cache = {}; int fib (n){ int calculations = 0; calculations++; if(cache.containsKey(n)){ return cache[n]; }else{ if(n < 2){ return n; }else{ cache[n] = fib(n-1) + fib(n-2); return cache[n]; } } } return (int n) { int result = fib(n); print("Total calculations: $calculations"); // Now prints correct value return result; }; } var memoized = memoizedAddTo80(); var faster = fibonacciMaster(); // print(fibonacci(8)); print(calculations); print(faster(8)); // print('1 ${memoized(5)}'); // print('2 ${memoized(4)}'); }
Editor Settings
Theme
Key bindings
Full width
Lines