Savoir si nombre premier ou non

Bonjour

Svp si c'est possible une macro pour savoir si c'est le nombre dans une cellule A1 est un nombre premier ou non

A1 = nombre exemple 7

A2= résultat premier Vs Non pas Premier

Merci

Bonjour,

Fonction personnalisée :

Function NB_Premier(NB As Long) As Boolean
Dim I As Long
If NB = 0 Or NB = 1 Then Exit Function 
For I = 2 To Sqr(NB) 
If NB Mod I = 0 Then Exit Function
Next I
Nb_Premier = True
End Function

@ bientôt

LouReeD

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

Merci a tous pour votre aide mais comment utiliser cela dans un macro avec une bouton ou autres??

J'ai coller le code dans un module mais rien ne fonctionne

Si le chiffre à tester est en A1

Sub Lance()
MsgBox (NB_Premier([A1]))
End Sub

procédure à lier au bouton

@ bientôt

LouReeD

Ca na pas fonctionner pour les autres fonction avec paramètres je pense

Salut à tous,

Une autre 'tite fonction personnalisée

Function estPremier(R As Range) As Boolean
Dim ok As Boolean, i As Byte

    ok = True
    For i = 2 To 6
        If (R.Value / i) - Int(R.Value / i) = 0 And Not i = R.Value Then ok = False
    Next i
    estPremier = ok
End Function

=>un nombre en A1, et en B1 on saisit =estPremier(A1)

Pierre

Aussi est-ce possible avec une formule est non pas macro??

Merci

Salut emiros, Salut le Forum,

formule matricielle dans A2:

=SI(A1=2;"premier";SI(ET(MOD(A1;LIGNE(INDIRECT("2:"&ARRONDI.SUP
(RACINE(A1);0))))<>0);"premier";"Non pas Premier"))
19emiros.xlsx (7.67 Ko)

Bonne nuit

Rechercher des sujets similaires à "savoir nombre premier"