1.dim a as integer dim b as integer 如何判断两个数能否整除? _________________________ if a/b=a\b then msgbox "a能被b整除"2.等会给你答案
'该函数判断x是否为素数 Private Function IsPrime(X As Long) As Boolean Dim Y As Long IsPrime = True For Y = 2 To Sqr(X) If X / Y = X \ Y Then IsPrime = False Y = X + 1 End If Next Y End Function'如下代码显示Num1的所有质因子: For Num2 = 2 To Num1 - 1 If IsPrime(Num2) = True Then Do While Num1 / Num2 = Num1 \ Num2 print.debug Num2 & vbcrlf Num1 = Num1 / Num2 Loop End If Next Num2
Public Function DivideExactly(ByVal pB As Long, ByVal pA As Long) As Boolean Dim tOutBool As Boolean
tOutBool = Not CBool(pB Mod pA)
DivideExactly = tOutBool End Function返回True tB能被tA整除 返回False tB不能被tA整除
下面是一个分解质因数的函数模块,但是其速度不如“狼行天下”版主写的同类函数快。2000000000 To 2000010000的10000个值分解质因数用了1秒(编译后)。 这个函数返回tpPrimeFactor类型,将质因数表示为素数的n次幂,也就是pfPrime的pfCount次幂。 NumberAnalyzePrimes是分解质因数的函数。它可以分解Long类型取值范围以内任意合法整数的质因数。'UltraPrimeNumbers模块 'KiteGirl[China] 2005'tpPrimeFactor 类型'NumberPrimeFactors 函数 'NumberIsPrime 函数 'PrimeNumbersGet 函数Option ExplicitPublic Type tpPrimeFactor pfPrime As Long '素数 pfCount As Byte '级数 End TypePrivate priPrimesTable() As Long ' Private priPrimesTable_Enabled As Boolean ' Private priPrimesTable_Length As Long 'Public Function NumberAnalyzePrimes(ByVal pNumber As Long) As tpPrimeFactor() Dim tOutDatas() As tpPrimeFactor Dim tOutDatas_Index As Long
PrimesTableCreate Dim tPrimesTable_Index As Long Dim tPrimesTable_Limit As Long
Dim tCheck_LoopEnd As Boolean Dim tCheck_FindPrime As Boolean Dim tPrimes As Long Dim tNumber As Long
PrimeNumbersGet = tOutLongs() End FunctionPrivate Sub PrimesTableCreate() If Not priPrimesTable_Enabled Then priPrimesTable() = PrimeNumbersGet(65536) priPrimesTable_Length = UBound(priPrimesTable()) priPrimesTable_Enabled = True End If End Sub
dim b as integer
如何判断两个数能否整除?
_________________________
if a/b=a\b then msgbox "a能被b整除"2.等会给你答案
Private Function IsPrime(X As Long) As Boolean
Dim Y As Long
IsPrime = True
For Y = 2 To Sqr(X)
If X / Y = X \ Y Then
IsPrime = False
Y = X + 1
End If
Next Y
End Function'如下代码显示Num1的所有质因子:
For Num2 = 2 To Num1 - 1
If IsPrime(Num2) = True Then
Do While Num1 / Num2 = Num1 \ Num2
print.debug Num2 & vbcrlf
Num1 = Num1 / Num2
Loop
End If
Next Num2
Dim tOutBool As Boolean
tOutBool = Not CBool(pB Mod pA)
DivideExactly = tOutBool
End Function返回True tB能被tA整除
返回False tB不能被tA整除
这个函数返回tpPrimeFactor类型,将质因数表示为素数的n次幂,也就是pfPrime的pfCount次幂。
NumberAnalyzePrimes是分解质因数的函数。它可以分解Long类型取值范围以内任意合法整数的质因数。'UltraPrimeNumbers模块
'KiteGirl[China] 2005'tpPrimeFactor 类型'NumberPrimeFactors 函数
'NumberIsPrime 函数
'PrimeNumbersGet 函数Option ExplicitPublic Type tpPrimeFactor
pfPrime As Long '素数
pfCount As Byte '级数
End TypePrivate priPrimesTable() As Long '
Private priPrimesTable_Enabled As Boolean '
Private priPrimesTable_Length As Long 'Public Function NumberAnalyzePrimes(ByVal pNumber As Long) As tpPrimeFactor() Dim tOutDatas() As tpPrimeFactor
Dim tOutDatas_Index As Long
PrimesTableCreate Dim tPrimesTable_Index As Long
Dim tPrimesTable_Limit As Long
Dim tCheck_LoopEnd As Boolean
Dim tCheck_FindPrime As Boolean
Dim tPrimes As Long
Dim tNumber As Long
tNumber = pNumber
tPrimesTable_Limit = Sqr(tNumber) + 1
For tPrimesTable_Index = 1 To priPrimesTable_Length
tPrimes = priPrimesTable(tPrimesTable_Index)
tCheck_LoopEnd = tPrimes > tPrimesTable_Limit
If tCheck_LoopEnd Then Exit For
tCheck_FindPrime = Not CBool(tNumber Mod tPrimes)
If tCheck_FindPrime Then
ReDim Preserve tOutDatas(tOutDatas_Index)
tOutDatas(tOutDatas_Index).pfPrime = tPrimes
Do
If Not CBool(tNumber Mod tPrimes) Then
With tOutDatas(tOutDatas_Index)
.pfCount = .pfCount + 1
End With
tNumber = tNumber \ tPrimes
Else
tPrimesTable_Limit = Sqr(tNumber) + 1
Exit Do
End If
Loop
tOutDatas_Index = tOutDatas_Index + 1
End If
Next
If tNumber > 1 Then
ReDim Preserve tOutDatas(tOutDatas_Index)
tOutDatas(tOutDatas_Index).pfPrime = tNumber
tOutDatas(tOutDatas_Index).pfCount = 1
End If
NumberAnalyzePrimes = tOutDatas()
End FunctionPublic Function NumberIsPrime(ByVal pNumber As Long) As Boolean
Dim tOutBool As Boolean
PrimesTableCreate Dim tPrimesTable_Index As Long
Dim tPrimesTable_Limit As Long
tPrimesTable_Limit = Sqr(pNumber) + 1
Dim tCheck_LoopEnd As Boolean
For tPrimesTable_Index = 1 To priPrimesTable_Length
tOutBool = CBool(pNumber Mod priPrimesTable(tPrimesTable_Index))
tCheck_LoopEnd = priPrimesTable(tPrimesTable_Index) > tPrimesTable_Limit
tCheck_LoopEnd = tCheck_LoopEnd Or (Not tOutBool)
If tCheck_LoopEnd Then Exit For
Next
NumberIsPrime = tOutBool
End FunctionPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
Dim tOutLongs() As Long
Dim tOutLongs_Index As Long
Dim tLineDatas() As Boolean
Dim tLineDatas_Index As Long
Dim tLineDatas_Index_Over As Long
ReDim tLineDatas(pLimit)
Dim tStep_Index As Long
Dim tStep_Start As Long
tLineDatas_Index_Over = Sqr(pLimit) + 1
For tLineDatas_Index = 2 To tLineDatas_Index_Over
If Not tLineDatas(tLineDatas_Index) Then
tStep_Start = tLineDatas_Index + tLineDatas_Index
For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
tLineDatas(tStep_Index) = True
Next
End If
Next
tOutLongs_Index = 2 ReDim Preserve tOutLongs(tOutLongs_Index)
tOutLongs(0) = 1: tOutLongs(1) = 2 For tLineDatas_Index = 3 To pLimit Step 2
If Not tLineDatas(tLineDatas_Index) Then
ReDim Preserve tOutLongs(tOutLongs_Index)
tOutLongs(tOutLongs_Index) = tLineDatas_Index
tOutLongs_Index = tOutLongs_Index + 1
End If
Next
PrimeNumbersGet = tOutLongs()
End FunctionPrivate Sub PrimesTableCreate()
If Not priPrimesTable_Enabled Then
priPrimesTable() = PrimeNumbersGet(65536)
priPrimesTable_Length = UBound(priPrimesTable())
priPrimesTable_Enabled = True
End If
End Sub