XL 2019 formule calcule

eCHO

XLDnaute Junior
bonjour

S’il vous plait j’aimerai savoir la formule qui va me permettre de définir combien des montants suivant (2.37 ; 7.39 ; 10.98 et 11.03) contient ce montant total (2740)

Pour mieux comprendre :

(X * 2.37) + (Y * 7.39) + (Z *10.98) + (W*11.09) = 2740

Et que ( x y z w ) sois un nombre entier sans virgule

Merci d’avance
 
Solution
Re,
Just for the fun .... en PJ la liste d'une solution possible pour chaque valeur de X en excluant une valeur nulle pour les variables. Ca en fait déjà 733.
Avec :
VB:
Sub Calcul()
Dim chaine$, x, y, z, w, T%, ix%, iy%, iz%, iw%, L%, reste, Go%
Range("A2:E1000").ClearContents: chaine = ""
x = 2.37: y = 7.39: z = 10.98: w = 11.09: T = 2740: L = 2: Go = 0
For ix = 1 To Int(T / x)
    Application.StatusBar = "Progression :  " & Format(ix / Int(T / x), "0.00%")
    For iy = 1 To Int(T / y)
        For iz = 1 To Int(T / z)
            For iw = 1 To Int(T / w)
                reste = T - ix * x - iy * y - iz * z - iw * w
                If reste < 0 Then Exit For  ' Si le reste est négatif, inutile de continuer au delà de cette valeur de iw...

sylvanu

XLDnaute Barbatruc
Supporter XLD
Bonjour Echo,
Comme c'est une équation à 4 inconnues, il n'existe pas de "formule" pour résoudre le problème.
Une solution bourrin avec :
VB:
Sub Calcul()
Dim chaine$, x, y, z, w, T%, ix%, iy%, iz%, iw%, reste
chaine = ""
x = 2.37: y = 7.39: z = 10.98: w = 11.09: T = 2740
For ix = 0 To Int(T / x)
For iy = 0 To Int(T / y)
For iz = 0 To Int(T / z)
For iw = 0 To Int(T / w)
    reste = T - ix * x - iy * y - iz * z - iw * w
    If reste = 0 Then
        chaine = chaine & Chr(10) & "X=" & ix & "  Y=" & iy & "  Z=" & iz & "  W=" & iw & "  Total= " & (ix * x + iy * y + iz * z + iw * w)
    End If
    ' limitation à 256 caractères pour la réponse, à modifier suivant besoin
    If Len(chaine) > 256 Then GoTo Fin
Next iw, iz, iy, ix
Fin:
MsgBox chaine
End Sub
qui donne dans l'état 8 solutions :
1633619123642.png

Si une seule solution vous suffit alors :
Code:
Sub Calcul()
Dim x, y, z, w, T%, ix%, iy%, iz%, iw%, reste
chaine = ""
x = 2.37: y = 7.39: z = 10.98: w = 11.09: T = 2740
For ix = 0 To Int(T / x)
For iy = 0 To Int(T / y)
For iz = 0 To Int(T / z)
For iw = 0 To Int(T / w)
    reste = T - ix * x - iy * y - iz * z - iw * w
    If reste = 0 Then
        MsgBox "X=" & ix & "  Y=" & iy & "  Z=" & iz & "  W=" & iw & "  Total= " & (ix * x + iy * y + iz * z + iw * w)
        Exit Sub
    End If
Next iw, iz, iy, ix
End Sub
1633619197742.png
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
Re,
Just for the fun .... en PJ la liste d'une solution possible pour chaque valeur de X en excluant une valeur nulle pour les variables. Ca en fait déjà 733.
Avec :
VB:
Sub Calcul()
Dim chaine$, x, y, z, w, T%, ix%, iy%, iz%, iw%, L%, reste, Go%
Range("A2:E1000").ClearContents: chaine = ""
x = 2.37: y = 7.39: z = 10.98: w = 11.09: T = 2740: L = 2: Go = 0
For ix = 1 To Int(T / x)
    Application.StatusBar = "Progression :  " & Format(ix / Int(T / x), "0.00%")
    For iy = 1 To Int(T / y)
        For iz = 1 To Int(T / z)
            For iw = 1 To Int(T / w)
                reste = T - ix * x - iy * y - iz * z - iw * w
                If reste < 0 Then Exit For  ' Si le reste est négatif, inutile de continuer au delà de cette valeur de iw
                If reste = 0 Then
                    Cells(L, 1) = ix: Cells(L, 2) = iy: Cells(L, 3) = iz: Cells(L, 4) = iw: Cells(L, 5) = ix * x + iy * y + iz * z + iw * w
                    L = L + 1
                    Go = 1 ' Si Go=1 on sort des boucles
                    Exit For
                End If
            Next iw
            If Go = 1 Then Exit For
        Next iz
        If Go = 1 Then Exit For
    Next iy
    Go = 0
Next ix
Fin:
Application.StatusBar = ""
End Sub
 

Pièces jointes

  • XYZW.xlsm
    52.3 KB · Affichages: 8

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour à tous,

Un essai pour trouver toutes les solutions. Cliquer sur le bouton Hop!
On trouve 13.870 quadruplets en environ 93 secondes sur ma bécane.
Le compteur dans la barre d'état accélère au fur et à mesure que l'exécution se déroule.
 

Pièces jointes

  • eCHO- Constituants total- v1.xlsm
    259.3 KB · Affichages: 11

Discussions similaires

Statistiques des forums

Discussions
311 725
Messages
2 081 943
Membres
101 849
dernier inscrit
florentMIG