Last active
February 24, 2023 16:46
-
-
Save poacosta/7c6cf73d1540b28d43fa85a327ceff92 to your computer and use it in GitHub Desktop.
n-th fibonacci (3 ways)
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
namespace Fibonacci; | |
/// <summary> | |
/// The Fibonacci sequence is a sequence of numbers where a number is the sum of the two preceding numbers. | |
/// The first 10 numbers in the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. | |
/// </summary> | |
public static class Fibonacci | |
{ | |
/// <summary> | |
/// Fibonacci in a recursive version | |
/// </summary> | |
/// <description> | |
/// The recursive version is the most straightforward way to implement the Fibonacci sequence. | |
/// Recursion is the process of defining a problem (or the solution to a problem) | |
/// in terms of (a simpler version of) itself. | |
/// </description> | |
/// <param name="n">Sequence position</param> | |
/// <returns>The number at the given sequence position.</returns> | |
public static int FibonacciRecursive(int n) | |
{ | |
return n switch | |
{ | |
< 0 => throw new ArgumentOutOfRangeException(nameof(n), | |
"The sequence position must be greater than or equal to zero."), | |
0 or 1 => n, | |
_ => FibonacciRecursive(n - 2) + FibonacciRecursive(n - 1) | |
}; | |
} | |
/// <summary> | |
/// Fibonacci, the iterative way | |
/// </summary> | |
/// <description> | |
/// The recursive version is the most straightforward way to implement the Fibonacci sequence. | |
/// A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. | |
/// </description> | |
/// <param name="n">Sequence position</param> | |
/// <returns>The number at the given sequence position.</returns> | |
public static int FibonacciLoop(int n) | |
{ | |
if (n < 0) | |
throw new ArgumentOutOfRangeException(nameof(n), | |
"The sequence position must be greater than or equal to zero."); | |
for (int i = 0, first = 0, second = 1; i <= n; i++, first += second, second = first - second) | |
if (i == n) | |
return first; | |
return n; | |
} | |
/// <summary> | |
/// Fibonacci in a tail-recursive version | |
/// </summary> | |
/// <description> | |
/// Tail recursion is defined as a recursive function in which | |
/// the recursive call is the last statement that is executed by the function. | |
/// So basically nothing is left to execute after the recursion call. | |
/// </description> | |
/// <param name="n">Sequence position</param> | |
/// <returns>The number at the given sequence position.</returns> | |
public static int FibonacciTail(int n) | |
{ | |
if (n < 0) | |
throw new ArgumentOutOfRangeException(nameof(n), | |
"The sequence position must be greater than or equal to zero."); | |
return FibonacciTailRecursive(n, 0, 1); | |
} | |
private static int FibonacciTailRecursive(int n, int first, int second) => | |
n == 0 ? first : FibonacciTailRecursive(n - 1, second, first + second); | |
} | |
internal static class MainClass | |
{ | |
private static void Main() | |
{ | |
Console.WriteLine($"Recursive: {Fibonacci.FibonacciRecursive(10)}"); | |
Console.WriteLine($"Iterative: {Fibonacci.FibonacciLoop(10)}"); | |
Console.WriteLine($"Tail-recursive: {Fibonacci.FibonacciTail(10)}"); | |
} | |
} |
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
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
// The Fibonacci sequence is a sequence of numbers where a number is the sum of the two preceding numbers. | |
// The first 10 numbers in the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. | |
// In this example we will calculate the n-th Fibonacci of the sequence using three different methods: | |
// 1. Recursive | |
fmt.Printf("Recursive: %v \n", fibonacciRecursive(10)) | |
// 2. Iterative | |
fmt.Printf("Iterative: %v \n", fibonacciLoop(10)) | |
// 3. Tail-recursive | |
fmt.Printf("Tail-recursive: %v \n", fibonacciTail(10)) | |
} | |
/* | |
------------- Fibonacci in a recursive version --------------------- | |
The recursive version is the most straightforward way to implement the Fibonacci sequence. | |
Recursion is the process of defining a problem (or the solution to a problem) | |
in terms of (a simpler version of) itself. | |
-------------------------------------------------------------------- | |
*/ | |
func fibonacciRecursive(n int) int { | |
if n == 0 || n == 1 { | |
return n | |
} | |
return fibonacciRecursive(n-2) + fibonacciRecursive(n-1) | |
} | |
/* | |
------------- Fibonacci, the iterative way ---------------------------- | |
The recursive version is the most straightforward way to implement the Fibonacci sequence. | |
A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. | |
------------------------------------------------------------------------ | |
*/ | |
func fibonacciLoop(n int) int { | |
var result int | |
for i, first, second := 0, 0, 1; i <= n; i, first, second = i+1, first+second, first { | |
if i == n { | |
result = first | |
} | |
} | |
return result | |
} | |
/* | |
------------- Fibonacci in a tail-recursive version --------------------- | |
Tail recursion is defined as a recursive function in which | |
the recursive call is the last statement that is executed by the function. | |
So basically nothing is left to execute after the recursion call. | |
------------------------------------------------------------------------- | |
*/ | |
func fibonacciTail(n int) int { | |
return fibonacciTailRecursive(n, 0, 1) | |
} | |
func fibonacciTailRecursive(n, first, second int) int { | |
if n == 0 { | |
return first | |
} | |
return fibonacciTailRecursive(n-1, second, first+second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment