Tuesday, November 3, 2009

Pool Table Minimum Clearances

prime factorization (VB)

Two agile functions for prime factorization of a number. Greetings
r


Dim T As Long Dim R As Long Dim S As String

'is based on the principle that a number R = Sqr(N)
T = N Mod 2 + 1
F = 1
If N = 1 Then S = N
Else
Do Until F > R
F = F + T
If (N) Mod F = 0 Then
S = S & F & "*"
N = N / F
F = F - T
R = Sqr(N)
End If
Loop
If N = 1 Then
S = Left(S, Len(S) - 1)
Else
S = S & N
End If
End If

Fattori_primi = S
'volendo una matrice sostituire con
'Fattori_primi = Split(S, "*")

End Function


Public Function PrimiB2(ByVal N As Long) As Long()
'di Nur
Dim arrF() As Long
Dim F As Long, NP As Long, d As Long
Dim Stp As Long, Radice As Long
If N = 1 Then
ReDim arrF(0)
arrF(0) = 1
Else
Radice = Sqr(N)
d = 1
Stp = N Mod 2 + 1
Do Until d > Radice
d = d + Stp
If N Mod d = 0 Then
NP = NP + 1
ReDim Preserve arrF(NP - 1)
arrF(NP - 1) = d
N = N / d
d = d - Stp
Radice = Sqr(N)
End If

If Loop ReDim Preserve arrF
d (NP)
arrF (NP) = N
ElseIf d = 1 Then ReDim
arrF (0)
arrF (0) = N
End If End If

PrimiB2 = arrF
End Function



0 comments:

Post a Comment