Author:水如烟
首先列出结果,共375位:
200 ,375 ,7886578673647905035523632139321850622951359776871732632947425332443594499634033429203042840119846239041772121389196
38830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853
27752424240757390324032125740557956866022603190417032406235170085879617892222278962370389737472000000000000000000000
0000000000000000000000000000
说它简陋,一是算法欠妥(不懂),二是没发挥计算机的计算能力(线程和服务)。只是,管它黑猫白猫,抓到老鼠就是了。
它的应用还是蛮大的,可以算递归数列,特别将它与数据库结合后,可以得到数表。现在我只是简单的算算阶乘。在100之内,它还是蛮快的。可能与我的计算机有关。300之后,就慢多了,不过数据位数已大于615位了。
测试:
Dim a As New UnitString
Dim b As New UnitString
Dim s As UnitString
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a.Value = " 1 "
For i As Integer = 1 To 300
b.Value = i.ToString
a = a * b
Next
Me .RichTextBox1.AppendText( String .Format( " {0,-4},{1,-4},{2} " , 300 , a.Length, a.Value) & vbCrLf)
End Sub
End Class
就两个文件。注释没写,不好意思。
UnitChar.vb
Private gCarry As Integer = 0
' '' <summary>
' '' 最后一次运算的进位
' '' </summary>
' '' <remarks>仅保存最后一次运算</remarks>
Public ReadOnly Property Carry() As Integer
Get
Return gCarry
End Get
End Property
Private gValue As Char = " 0 " c
' '' <summary>
' '' 值
' '' </summary>
Public Property Value() As Char
Get
Return gValue
End Get
Set ( ByVal value As Char )
gValue = value
End Set
End Property
' '' <summary>
' '' 副本
' '' </summary>
Public Function Clone() As UnitChar
Dim mResult As New UnitChar
With mResult
.gValue = Me .gValue
.gCarry = Me .gCarry
End With
Return mResult
End Function
' '' <summary>
' '' 整数
' '' </summary>
Public Function ToInteger() As Integer
Return CType (gValue.ToString, Integer )
End Function
' 进位清零
Private Sub ClearCarry()
gCarry = 0
End Sub
Public Sub Add( ByVal unit As UnitChar)
Me .ClearCarry()
Dim mAddResult() As Char
mAddResult = ( Me .ToInteger + unit.ToInteger).ToString.ToCharArray
If mAddResult.Length = 2 Then
Me .gCarry = CType (mAddResult( 0 ).ToString, Integer )
Me .gValue = mAddResult( 1 )
Else
Me .gValue = mAddResult( 0 )
End If
End Sub
Public Sub Multiply( ByVal unit As UnitChar)
Me .ClearCarry()
Dim mAddResult() As Char
mAddResult = ( Me .ToInteger * unit.ToInteger).ToString.ToCharArray
If mAddResult.Length = 2 Then
Me .gCarry = CType (mAddResult( 0 ).ToString, Integer )
Me .gValue = mAddResult( 1 )
Else
Me .gValue = mAddResult( 0 )
End If
End Sub
Public Shared Operator + ( ByVal a As UnitChar, ByVal b As UnitChar) As UnitChar
Dim mResult As UnitChar = a.Clone
mResult.Add(b)
Return mResult
End Operator
Public Shared Operator * ( ByVal a As UnitChar, ByVal b As UnitChar) As UnitChar
Dim mResult As UnitChar = a.Clone
mResult.Multiply(b)
Return mResult
End Operator
End Class
UnitString.vb
' 进位
Protected gCarry As Integer = 0
Protected gOriginalValue As String
Protected gCurrentUniChars() As UnitChar
Protected gLength As Integer
Public ReadOnly Property Length() As Integer
Get
Return gLength
End Get
End Property
Private gValue As String = " 0 "
' '' <summary>
' '' 值
' '' </summary>
Public Property Value() As String
Get
Dim mBuilder As New System.Text.StringBuilder
If gCarry > 0 Then
mBuilder.Append(gCarry.ToString)
End If
For i As Integer = 0 To Me .gLength - 1
mBuilder.Append( Me .gCurrentUniChars(i).Value)
Next
Return mBuilder.ToString
End Get
Set ( ByVal value As String )
Me .gValue = value
Me .gOriginalValue = value
Initialize()
End Set
End Property
Public ReadOnly Property NewUnitString() As UnitString
Get
Dim mResult As New UnitString
mResult.Value = Me .Value
Return mResult
End Get
End Property
Private Sub Initialize()
Me .ClearCarry()
Me .gCurrentUniChars = ToUnitChars( Me .gOriginalValue)
Me .gLength = Me .gCurrentUniChars.Length
End Sub
Private Function ToUnitChars( ByVal value As String ) As UnitChar()
Dim mChars() As Char = value.ToCharArray
Dim mResult(mChars.Length - 1 ) As UnitChar
For i As Integer = 0 To mChars.Length - 1
mResult(i) = New UnitChar
mResult(i).Value = mChars(i)
Next
Return mResult
End Function
' '' <summary>
' '' 副本
' '' </summary>
Public Function Clone() As UnitString
Dim mResult As New UnitString
With mResult
.Value = Me .Value
End With
Return mResult
End Function
' 进位清零
Private Sub ClearCarry()
gCarry = 0
End Sub
Public Sub Add( ByVal unit As UnitString)
Dim mValue1 As UnitString
Dim mValue2 As UnitString
If Me .Length >= unit.Length Then
mValue1 = Me
mValue2 = unit
Else
mValue1 = unit
mValue2 = Me
End If
Dim tmpUnitChars() As UnitChar = Me .ToUnitChars(mValue2.Value)
For i As Integer = mValue2.Length - 1 To 0 Step - 1
mValue1.AddChar(tmpUnitChars(mValue2.Length - i - 1 ), i)
Next
Me .Value = mValue1.Value
End Sub
Public Sub Multiply( ByVal unit As UnitString)
Dim mValue1 As UnitString
Dim mValue2 As UnitString
If Me .Length >= unit.Length Then
mValue1 = Me .Clone
mValue2 = unit.Clone
Else
mValue1 = unit.Clone
mValue2 = Me .Clone
End If
Dim tmpUnitStrings(mValue2.Length - 1 ) As UnitString
Dim tmpUnitChars() As UnitChar = Me .ToUnitChars(mValue2.Value)
For i As Integer = 0 To mValue2.Length - 1
tmpUnitStrings(i) = mValue1.Clone
tmpUnitStrings(i).MultiplyChar(tmpUnitChars(i))
If tmpUnitStrings(i).gCarry > 0 Then
tmpUnitStrings(i) = tmpUnitStrings(i).NewUnitString
End If
Next
Dim mResult As New UnitString
Dim tmp As UnitString
For i As Integer = 0 To mValue2.Length - 1
mResult = mResult.NewUnitString
tmp = New UnitString
tmp.Value = tmpUnitStrings(i).Value & New String ( " 0 " c, mValue2.Length - i - 1 )
mResult.Add(tmp)
Next
Me .Value = mResult.Value
End Sub
Public Shared Operator + ( ByVal a As UnitString, ByVal b As UnitString) As UnitString
Dim mResult As UnitString = a.Clone
mResult.Add(b)
Return mResult
End Operator
Public Shared Operator * ( ByVal a As UnitString, ByVal b As UnitString) As UnitString
Dim mResult As UnitString = a.Clone
mResult.Multiply(b)
Return mResult
End Operator
Protected Overridable Sub AddChar( ByVal unit As UnitChar, ByVal OperateDigit As Integer )
Dim mCurrentUnitChar As UnitChar = Me .gCurrentUniChars( Me .gLength - OperateDigit - 1 )
mCurrentUnitChar.Add(unit)
If mCurrentUnitChar.Carry > 0 Then
If Me .gLength - OperateDigit - 1 = 0 Then
Me .gCarry += mCurrentUnitChar.Carry
Else
Dim tmp As New UnitChar
tmp.Value = CType (mCurrentUnitChar.Carry.ToString, Char )
AddChar(tmp, OperateDigit + 1 )
End If
End If
End Sub
Protected Overridable Sub MultiplyChar( ByVal unit As UnitChar)
For i As Integer = Me .gLength - 1 To 0 Step - 1
MultiplyCharSingle(unit, i)
Next
End Sub
Protected Overridable Sub MultiplyCharSingle( ByVal unit As UnitChar, ByVal OperateDigit As Integer )
Dim mCurrentUnitChar As UnitChar = Me .gCurrentUniChars( Me .gLength - OperateDigit - 1 )
mCurrentUnitChar.Multiply(unit)
If mCurrentUnitChar.Carry > 0 Then
If Me .gLength - OperateDigit - 1 = 0 Then
Me .gCarry += mCurrentUnitChar.Carry
Else
Dim tmp As New UnitChar
tmp.Value = CType (mCurrentUnitChar.Carry.ToString, Char )
AddChar(tmp, OperateDigit + 1 )
End If
End If
End Sub
End Class
一并列出测试加、乘:
Dim a As New UnitString
Dim b As New UnitString
Dim s As UnitString
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
a.Value = New String ( " 9 " c, CInt (Math.Pow( 2 , 10 ))) ' 相加超过14就不行了,相乘没试过。
b.Value = New String ( " 9 " c, CInt (Math.Pow( 2 , 10 )))
s = a + b
Me .RichTextBox1.AppendText(a.Value & vbCrLf)
Me .RichTextBox1.AppendText( " + " & vbCrLf)
Me .RichTextBox1.AppendText(b.Value & vbCrLf)
Me .RichTextBox1.AppendText( " = " & vbCrLf)
Me .RichTextBox1.AppendText(s.Value & vbCrLf)
s = a * b
Me .RichTextBox1.AppendText(a.Value & vbCrLf)
Me .RichTextBox1.AppendText( " * " & vbCrLf)
Me .RichTextBox1.AppendText(b.Value & vbCrLf)
Me .RichTextBox1.AppendText( " = " & vbCrLf)
Me .RichTextBox1.AppendText(s.Value & vbCrLf)
End Sub
End Class
结果:
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999
+
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999
=
19999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999998
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999
*
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999999999999999
=
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999800000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000001