Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Last active November 3, 2022 17:52
Show Gist options
  • Save RandyMcMillan/59d67e41dc2a59cbbe63528752604c62 to your computer and use it in GitHub Desktop.
Save RandyMcMillan/59d67e41dc2a59cbbe63528752604c62 to your computer and use it in GitHub Desktop.
Fibonacci
// Copyright 2022 @RandyMcmillan. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'dart:math';
void main() {
var i = 5;
var p = pow(2,i);
print('fibonacci($i) = ${fibonacci(i)}');
print('fibonacci2($p) = ${fibonacci2(p)}');
}
/// Computes the nth Fibonacci number.
int fibonacci(int n) {
return n < 2 ? n : (fibonacci(n - 1) + fibonacci(n - 2));
}
num fibonacci2(num n) {
return n < 2 ? n : (fibonacci2(n - 1) + fibonacci2(n - 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment