Bonjour,
Très simple.
Function IsPrime(Number As Long) As Boolean
'fonction de détermination de primalité
' ? IsPrime(7)
Dim i As Long
If Int(Number / 2) = (Number / 2) Then
Exit Function
Else
For i = 3 To Sqr(Number) Step 2
If Int(Number / i) = (Number / i) Then Exit Function
Next i
End If
IsPrime = True
End Function
Pour obtenir une liste des nombres premiers :
Public Function Liste_Premiers(max As Long) As Long()
'liste tous les nombres premiers jusqu'à la valeur max (limitée...)
Dim liste() As Long, TEMP() As Boolean
Dim cpt As Long, rac As Long, i As Long, j As Long
ReDim TEMP(2 To max), liste(max \ 2)
rac = Sqr(max)
For i = 3 To rac Step 2
If TEMP(i) = False Then
For j = i * i To max Step i
TEMP(j) = True
Next
End If
Next i
liste(0) = 2
For i = 3 To max Step 2
If TEMP(i) = False Then
cpt = cpt + 1
liste(cpt) = i
End If
Next i
ReDim Preserve liste(cpt)
Liste_Premiers = liste
Erase TEMP, liste
End Function
Avec cette fonction de liste des premiers, tu peux ensuite utiliser les fonctions suivantes (que je te laisse découvrir) :
Public Function PrimesBeetween(min As Long, max As Long) As Long()
'Liste les nombres premiers entre 2 bornes
Dim PRIMES() As Long, TEMP() As Long, i As Long, cpt As Long
PRIMES = Liste_Premiers(max)
ReDim TEMP(LBound(PRIMES) To UBound(PRIMES))
For i = LBound(PRIMES) To UBound(PRIMES)
If PRIMES(i) > max Then Exit For
If PRIMES(i) >= min Then
TEMP(cpt) = PRIMES(i)
cpt = cpt + 1
End If
Next i
ReDim Preserve TEMP(cpt - 1)
PrimesBeetween = TEMP
Erase PRIMES, TEMP
End Function
Public Function PrimesBeetweenTh(first As Long, last As Long) As Long()
Dim PRIMES() As Long, TEMP() As Long, i As Long, cpt As Long
PRIMES = Liste_Premiers(last * last)
ReDim TEMP(LBound(PRIMES) To UBound(PRIMES))
For i = first To last
TEMP(cpt) = PRIMES(i)
cpt = cpt + 1
Next i
ReDim Preserve TEMP(cpt - 1)
PrimesBeetweenTh = TEMP
Erase PRIMES, TEMP
End Function
Public Function NumbersOfPrimesBeetween(first As Long, last As Long) As Long
NumbersOfPrimesBeetween = UBound(PrimesBeetween(first, last)) + 1
End Function