Tuesday, January 6, 2009
Fibonacci Sequence [Iterative & Recursive]
Fibonacci Sequence / Deret Fibonacci adalah suatu deret matematika yang berasal dari penjumlahan dua bilangan sebelumnya, Algoritma ini ditemukan oleh Leonardo Pissano FibonacciDi bawah ini adalah algoritma berikut sintaksnya untuk Fibonacci sequence versi iterative dan recursive di VB.Net / Visual Basic 2008 :
Public Class FibonacciSequence
Dim angka() As Long
Private Sub tombolfibonacci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tombolfibonacci.Click
Dim n As Integer
ReDim angka(Val(TextBox1.Text) - 1)
'Aktifkan salah satu fungsi yang ingin dipakai
'=====================================
'Iteratif_fibonacci()
'rekursif_fibonacci(0)
'=====================================
End Sub
'Versi Iterative Fibonacci
Function Iteratif_fibonacci()
Dim i As Integer
For i = 0 To angka.Length - 1 Step +1
If (i = 0 Or i = 1) Then
angka(i) = i
Else
angka(i) = angka(i - 1) + angka(i - 2)
End If
Next i
For i = 0 To angka.Length - 1 Step +1
TextBox2.Text = TextBox2.Text & angka(i) & " , "
Next i
End Function
'Versi Recursive Fibonacci
Function rekursif_fibonacci(ByVal i As Integer)
If i < angka.Length Then
If (i = 0 Or i = 1) Then
angka(i) = i
Else
angka(i) = angka(i - 1) + angka(i - 2)
End If
TextBox2.Text = TextBox2.Text & angka(i) & " , "
i = i + 1
rekursif_fibonacci(i)
Else
Exit Function
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End Sub
End Class
Label:
[VB.Net] Basic Algorithm
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment