MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / NthFibonacci

Function NthFibonacci

dynamic/fibonacci.go:10–25  ·  view source on GitHub ↗

https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/ NthFibonacci returns the nth Fibonacci Number

(n uint)

Source from the content-addressed store, hash-verified

8
9// NthFibonacci returns the nth Fibonacci Number
10func NthFibonacci(n uint) uint {
11 if n == 0 {
12 return 0
13 }
14
15 // n1 and n2 are the (i-1)th and ith Fibonacci numbers, respectively
16 var n1, n2 uint = 0, 1
17
18 for i := uint(1); i < n; i++ {
19 n3 := n1 + n2
20 n1 = n2
21 n2 = n3
22 }
23
24 return n2
25}

Callers 3

TestNthFibonacciFunction · 0.92
BenchmarkNthFibonacciFunction · 0.92
Test_NthFibonacciFunction · 0.85

Calls

no outgoing calls

Tested by 3

TestNthFibonacciFunction · 0.74
BenchmarkNthFibonacciFunction · 0.74
Test_NthFibonacciFunction · 0.68