Created with <3 with dartpad.dev.
Last active
November 3, 2022 17:52
-
-
Save RandyMcMillan/59d67e41dc2a59cbbe63528752604c62 to your computer and use it in GitHub Desktop.
Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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