package main
import "fmt"
// Fibonacci Sequence is 0 1 1 2 3 5 8 13 21 .......
// Read more on
// https://www.mathsisfun.com/numbers/fibonacci-sequence.html#:~:text=It%20is%20that%20simple!,out%20the%20next%20few%20numbers%3F
func main() {
n := 10
a := 0
b := 1
c := 0
for i := 1; i <= n; i++ {
fmt.Printf("%d ", a)
c = a + b
a = b
b = c
}
}
/*
Logic explaination
Algorithm
Add 'a + b', Store into 'c'
Swap current 'b's value into 'a'
Swap current 'c's value into 'b'
print 'a' while N th term
Initially a = 0, b = 1 , c = 0
First interation
fmt.Printf("%d ", a) : 0
Second itertaion
a = 0
b = 1
c = a + b , c = 1
a = b , a = 1
b = c , b = 1
fmt.Printf("%d ", a): 0 1
Third iteration
a = 1
b = 1
c = a + b , c = 2
a = b , a = 1
b = c , b = 2
fmt.Printf("%d ", a): 0 1 1
Fourth iteration
a = 1
b = 2
c = a + b , c = 3
a = b , a = 2
b = c , b = 3
fmt.Printf("%d ", a): 0 1 1 2
Fifth iteration
a = 2
b = 3
c = a + b ,c = 5
a = b , a = 3
b = c , b = 5
fmt.Printf("%d ", a) : 0 1 1 2 3
Sixth iteration
a = 3
b = 5
c = a + b ,c = 8
a = b , a = 5
b = c , b = 8
fmt.Printf("%d ", a) : 0 1 1 2 3 5
Seventh iteration
a = 5
b = 8
c = a + b , c = 13
a = b , a = 8
b = c , b = 13
fmt.Printf("%d ", a) : 0 1 1 2 3 5 8
And So on upto N th term
*/
Comments
Post a Comment