TheDeveloperBlog.com

Home | Contact Us

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

<< Back to VBNET

VB.NET Fibonacci Sequence

Implement the Fibonacci sequence. Each number is the sum of the previous two in this sequence.
Fibonacci sequence. The Fibonacci sequence has many uses. In this sequence, each number is the sum of the previous two numbers. In the 1200s Fibonacci published the sequence, showing its use in collecting interest.
Example. This is an iterative implementation of the Fibonacci sequence, where we compute numbers in a loop. We use a temporary variable (helpfully called "temp") to copy the previous number. This involves no recursion.

Tip: The Fibonacci method returns a number within the Fibonacci sequence, by its index.

However: You could store all the numbers in a List for better lookup performance. This eliminates the entire loop after initialization.

Note: The output matches the Fibonacci sequence. And we can add the two previous numbers up to check the sequence mentally.

Fibonacci: Wikipedia
VB.NET program that generates Fibonacci sequence Module Module1 Function Fibonacci(ByVal n As Integer) As Integer Dim a As Integer = 0 Dim b As Integer = 1 ' Add up numbers. For i As Integer = 0 To n - 1 Dim temp As Integer = a a = b b = temp + b Next Return a End Function Sub Main() ' Display first 10 Fibonacci numbers. For i As Integer = 0 To 10 Console.WriteLine(Fibonacci(i)) Next End Sub End Module Output 0 1 1 2 3 5 8 13 21 34 55
We use a Function, not a Sub, because a return value is required. The key concern here is the excessive looping. We run the same loop over and over again. If we cached the values in a list (a lookup table) this would be faster.Function
Summary. A recursive method can be used to compute Fibonacci numbers: each value is defined as the sum of the previous two. But an iterative solution is often easier to read and understand. Both versions help us understand the ideas involved.

Iterative: This style of code involves loops. We step forward through an operation with for-loops and index variables.

For Each, For

Recursive: This style uses method calls. A recursive method calls itself, so we define a method in terms of itself.

Recursion
© 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