Skip to content

Instantly share code, notes, and snippets.

@vikrambiwal
Created February 27, 2020 07:57
Show Gist options
  • Save vikrambiwal/29d7e8f0c9bf2d0687047c9bd1cb5d4a to your computer and use it in GitHub Desktop.
Save vikrambiwal/29d7e8f0c9bf2d0687047c9bd1cb5d4a to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"time"
)
func main() {
for i := 0; i <= 10; i++ {
index, value := fibonacciIndex(i)
fmt.Printf("Index: %d, Value: %d \n", index, value)
}
index, value := fibonacciValue(3)
fmt.Printf("\n Index: %d, Value: %d \n", index, value)
}
//fibonacciValue pass value and returns index and value of the next element of fibonacci series
func fibonacciValue(val int) (index, value int) {
i := 0
t1 := 0
t2 := 1
nextTerm := 0
if val == 0 {
return 1, 1
}
for i = 0; nextTerm < val+1; i++ {
if i == 0 {
fmt.Print(" ", t1)
continue
}
if i == 1 {
fmt.Print(" ", t2)
continue
}
nextTerm = t1 + t2
t1 = t2
t2 = nextTerm
fmt.Print(" ", nextTerm)
}
return i - 1, nextTerm
}
//fibonacciIndex pass index and returns index and value of the next element of fibonacci series
func fibonacciIndex(indx int) (index, value int) {
i := 0
t1 := 0
t2 := 1
nextTerm := 0
for i = 0; i <= indx; i++ {
if i == 0 {
continue
}
nextTerm = t1 + t2
if i == 1 {
continue
}
t1 = t2
t2 = nextTerm
}
return indx, nextTerm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment