VB.Net Tutorial/Class Module/Recursive Function — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:55, 26 мая 2010
Demonstrating Fibonacci sequence recursively
Public Class Tester
Public Shared Sub Main
Dim number As Integer = 13
Console.WriteLine("Fibonacci Value is " & Fibonacci(number))
End Sub
" calculate Fibonacci value recusively
Shared Function Fibonacci(ByVal number As Integer) As Long
" check for base cases
If number = 1 OrElse number = 0 Then
Return number
Else
Return Fibonacci(number - 1) + Fibonacci(number - 2)
End If
End Function " Fibonacci
End Class
Fibonacci Value is 233
Recursively generates factorial of number
Public Class Tester
Public Shared Sub Main
Dim value As Integer = 12
Dim i As Integer
For i = 0 To value
Console.WriteLine(i & "! = " & Factorial(i))
Next
End Sub
Shared Function Factorial(ByVal number As Long) As Long
If number <= 1 Then " base case
Return 1
Else
Return number * Factorial(number - 1)
End If
End Function " Factorial
End Class
0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600