TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Fibonacci Sequence Example

Compute the Fibonacci sequence with an iterative method. Use a for-loop.
Fibonacci. This sequence of numbers occurs in nature, and has many uses, even in finance. In a Fibonacci sequence, each number is equal to the previous two numbers added together.
With an iterative method, we quickly generate the Fibonacci sequence. Examples sometimes use recursion, but this has no practical benefit.
Example method. We introduce the func fibonacci(), which receives an int and returns another int. Fibonacci uses three variables—these store the result and help with addition.Func

For: We use a for-loop to iterate until the desired position in the sequence. We continually add up numbers until we reach that position.

For

Return: We return the Fibonacci number at the designated position. This is an int value.

Golang program that computes Fibonacci sequence package main import "fmt" func fibonacci(n int) int { a := 0 b := 1 // Iterate until desired position in sequence. for i := 0; i < n; i++ { // Use temporary variable to swap values. temp := a a = b b = temp + a } return a } func main() { // Display first 15 Fibonacci numbers. for n := 0; n < 15; n++ { // Compute number. result := fibonacci(n) fmt.Printf("Fibonacci %v = %v\n", n, result) } } Output Fibonacci 0 = 0 Fibonacci 1 = 1 Fibonacci 2 = 1 Fibonacci 3 = 2 Fibonacci 4 = 3 Fibonacci 5 = 5 Fibonacci 6 = 8 Fibonacci 7 = 13 Fibonacci 8 = 21 Fibonacci 9 = 34 Fibonacci 10 = 55 Fibonacci 11 = 89 Fibonacci 12 = 144 Fibonacci 13 = 233 Fibonacci 14 = 377
Some comments. This code is not optimal. The method computes a Fibonacci number at a position in the sequence. But for repeated reuse, memoization (caching) would be better.

Tip: To memoize this sequence, store the Fibonacci numbers in an array or slice. Then access those values when needed.

ArraySlice

Performance: Using a cache in this situation is far faster, especially with larger positions within the sequence.

A summary. Fibonacci numbers are easily computed in modern programming languages. This task helps us learn about numbers and programming languages. This makes more complex tasks easier.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf