ok,
Code: Select all
Function factorial(ByVal a As Long) As Long
factorial = 0
If a = 0 Or a = 1 Then
factorial = 1
Exit Function
ElseIf a > 1 Then
For j As Long = a To 2 Step -1
a = (j - 1) * a
factorial = a
Next
End If
End Function
Code: Select all
Function factorial(ByVal a As Integer) As Integer
factorial = 0
If a = 0 Or a = 1 Then
factorial = 1
Exit Function
ElseIf a > 1 Then
For j As Integer = a To 2 Step -1
a = Convert.ToInt32(j - 1) * a
factorial = a
Next
End If
End Function
If I use long, it only does factorials of up to 20, and if I use integers, it only does factorials of up to 12. After that, it crashes, and if I'm in debugging mode it says "OverflowException was unhandled" and "Arithmetic operation resulted in an overflow".
I know what error handling is and how to use it, but I'm not sure how to handle this error. When I use double, it doesn't do this, hence why I've used it, but the only annoying this is that when the factorial is too high, it is stored and displayed as an exponential (because of what a "double" variable is; a double-precision number stored as an exponential). This happens to me when I use odditude's:
Code: Select all
Function factorial2(ByVal x As Long) As Long
factorial2 = 1
If x < 2 Then Exit Function
For i As Long = x to 1 Step -1
factorial2 *= i
Next
End Function
code (that is, only displays factorials of up to 20, since he's using long integers).
I'm being serious, can you guys help?
As a side note, I should also post something that makes me happy, to balance things out (I've added ISBN10 to ISBN13 conversion to my ISBN10/ISBN13 validator):
Code: Select all
Option Strict On
Option Explicit On
Module Module1
Dim sISBN13 As String = ""
Dim sISBN10 As String = ""
Function bVerifySum13(ByRef usISBN13 As String) As Boolean
Dim iMax, iSum As Integer
bVerifySum13 = True
iMax = 0
If usISBN13 Like "#############" Then
For j As Integer = 2 To 12 Step 2
iMax = iMax + (Convert.ToInt32(Mid(usISBN13, (j - 1), 1))) + (3 * (Convert.ToInt32(Mid(usISBN13, j, 1))))
Next
iSum = (10 - (iMax Mod 10)) Mod 10
If iSum <> Convert.ToInt32(Mid(usISBN13, 13, 1)) Then
bVerifySum13 = False
End If
Else
bVerifySum13 = False
End If
End Function
Function bVerifySum10(ByRef usISBN10 As String) As Boolean
Dim iMax As Integer
iMax = 0
bVerifySum10 = True
If usISBN10 Like "##########" Or usISBN10.Length = 10 And Mid(usISBN10, 1, 9) Like "#########" And Mid(usISBN10, 10, 1) = "x" Then
For j = 10 To 2 Step -1
iMax = iMax + ((Convert.ToInt32(Mid(usISBN10, (10 - (j - 1)), 1))) * j)
Next
If Mid(usISBN10, 10, 1) <> "x" Then
iMax = iMax + Convert.ToInt32(Mid(usISBN10, 10, 1))
Else
iMax = iMax + 10
End If
If (iMax Mod 11) <> 0 Then
bVerifySum10 = False
End If
Else
bVerifySum10 = False
End If
End Function
Function sConvertN10toN13(ByRef usISBN10 As String) As String
Dim iMax As Integer = 0
sConvertN10toN13 = "978" + Mid(usISBN10, 1, 9)
For j As Integer = 2 To 12 Step 2
iMax = (iMax + (Convert.ToInt32(Mid(sConvertN10toN13, (j - 1), 1))) + (3 * (Convert.ToInt32(Mid(sConvertN10toN13, j, 1)))))
Next
sConvertN10toN13 = sConvertN10toN13 + ((10 - (iMax Mod 10)) Mod 10).ToString
End Function
Sub Main()
Dim sInputChoice As String = ""
Call DisplayOptions()
Do Until sInputChoice = "quit"
Call Console.Write("?: ")
sInputChoice = Console.ReadLine()
Select Case sInputChoice
Case "quit"
End
Case "disp"
Call DisplayOptions()
Case "10"
Call InterfaceISBN10()
Case "13"
Call InterfaceISBN13()
Case "10to13"
Call InterfaceISBN10ToISBN13()
Case "13to10"
Call InterfaceISBN13ToISBN10()
Case Else
Call Console.WriteLine("Invalid option")
End Select
Loop
End
End Sub
Sub InterfaceISBN10()
Call Console.WriteLine()
Call Console.WriteLine("ISBN-10 Validation")
Call Console.WriteLine("(To exit back to main menu, type 'r' and hit return)")
Do Until sISBN10 = "r"
Call Console.Write("?10: ")
sISBN10 = Console.ReadLine()
sISBN10 = sISBN10.ToLower
If sISBN10 <> "r" And bVerifySum10(sISBN10) <> True Then
Call Console.WriteLine("Invalid ISBN-10 number")
ElseIf sISBN10 <> "r" And bVerifySum10(sISBN10) = True Then
Call Console.WriteLine("Valid ISBN-10 number")
End If
Loop
Call Console.WriteLine()
Call DisplayOptions()
End Sub
Sub InterfaceISBN13()
Call Console.WriteLine()
Call Console.WriteLine("ISBN-13 Validation")
Call Console.WriteLine("(To exit back to main menu, type 'r' and hit return)")
Do Until sISBN13 = "r"
Call Console.Write("?13: ")
sISBN13 = Console.ReadLine()
sISBN13 = sISBN13.ToLower
If sISBN13 <> "r" And bVerifySum13(sISBN13) <> True Then
Call Console.WriteLine("Invalid ISBN-13 number")
ElseIf sISBN13 <> "r" And bVerifySum13(sISBN13) = True Then
Call Console.WriteLine("Valid ISBN-13 number")
End If
Loop
Call Console.WriteLine()
Call DisplayOptions()
End Sub
Sub InterfaceISBN10ToISBN13()
Call Console.WriteLine("ISBN10 to ISBN13 conversion")
Call Console.WriteLine("(To exit back to the main menu, type 'r' and hit return)")
Do
Call Console.Write("?10: ")
sISBN10 = Console.ReadLine()
If sISBN10 <> "r" And bVerifySum10(sISBN10) <> True Then
Call Console.WriteLine("Invalid input")
ElseIf sISBN10 <> "r" And bVerifySum10(sISBN10) = True Then
Call Console.WriteLine("As an ISBN13 number, this is " + sConvertN10toN13(sISBN10))
Call System.Threading.Thread.Sleep(1200)
Call Console.WriteLine("Verification of newly converted ISBN13 number...")
Call System.Threading.Thread.Sleep(300)
If bVerifySum13(sConvertN10toN13(sISBN10)) <> True Then
Call Console.WriteLine("Oops, it seems that this is invalid")
Else
Call Console.WriteLine("Everything A-OK")
End If
End If
Loop Until sISBN10 = "r"
End Sub
Sub InterfaceISBN13ToISBN10()
Call Console.WriteLine("This subprocedure has not been written yet,")
Call Console.WriteLine("but I put the skeleton here for the sake of")
Call Console.WriteLine("preparation.")
End Sub
Sub DisplayOptions()
Call Console.WriteLine("Options:")
Call Console.WriteLine("10 - ISBN10 validation")
Call Console.WriteLine("13 - ISBN13 validation")
Call Console.WriteLine("10to13 - ISBN10 to ISBN13 conversion")
Call Console.WriteLine("13to10 - ISBN13 to ISBN10 conversion (not yet written)")
Call Console.WriteLine("disp - display these options")
Call Console.WriteLine("quit - exit this program")
End Sub
End Module