IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Atteindre un chiffre donné en X mois de façon progressive.


Sujet :

Langage PHP

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Points : 264
    Points
    264
    Par défaut Atteindre un chiffre donné en X mois de façon progressive.
    Bonjour,

    Je voudrais atteindre un chiffre donné en X mois de façon progressive.

    Voilà ce à quoi j'ai pensé comme algo, avec 500 000 le chiffre donné et 30, le nombre de mois.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    $Total = 500000;
    $totalMonth = 30;
    echo 'Total : '.number_format($Total, 0, '.', ' ').PHP_EOL;
    echo 'Total month:'.$totalMonth.PHP_EOL;
    $i = 0;
    $totalCreated = 0;
    $start = intval($totalMonth/2) + intval($totalMonth*0.3);
    $operator = 'divide';
    $lastMonths = intval($totalMonth*0.2);
    while($i < $totalMonth) {
        if($operator == 'divide') {
            $Month = intval(($Total/$totalMonth)/$start);
            $start--;
        } else {
            $Month = intval(($Total/$totalMonth)*$start);
            $start++;
        }
        if($i == ($totalMonth - 1)) {
            //$Month = $Total - $totalCreated;
        }
        $totalCreated += $Month;
        if($start == 0) {
            $start = 1;
            $operator = 'multiply';
        }
     
        echo 'Month '.$i.' ('.$start.' | '.$operator.'): '.number_format($Month, 0, '.', ' ').PHP_EOL;
        $i++;
    }
     
    echo 'Total Created:'.number_format($totalCreated, 0, '.', ' ');
    Ce qui donne au final:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
    Total : 500 000
    Total month:30
    Month 0 (23 | divide): 694
    Month 1 (22 | divide): 724
    Month 2 (21 | divide): 757
    Month 3 (20 | divide): 793
    Month 4 (19 | divide): 833
    Month 5 (18 | divide): 877
    Month 6 (17 | divide): 925
    Month 7 (16 | divide): 980
    Month 8 (15 | divide): 1 041
    Month 9 (14 | divide): 1 111
    Month 10 (13 | divide): 1 190
    Month 11 (12 | divide): 1 282
    Month 12 (11 | divide): 1 388
    Month 13 (10 | divide): 1 515
    Month 14 (9 | divide): 1 666
    Month 15 (8 | divide): 1 851
    Month 16 (7 | divide): 2 083
    Month 17 (6 | divide): 2 380
    Month 18 (5 | divide): 2 777
    Month 19 (4 | divide): 3 333
    Month 20 (3 | divide): 4 166
    Month 21 (2 | divide): 5 555
    Month 22 (1 | divide): 8 333
    Month 23 (1 | multiply): 16 666
    Month 24 (2 | multiply): 16 666
    Month 25 (3 | multiply): 33 333
    Month 26 (4 | multiply): 50 000
    Month 27 (5 | multiply): 66 666
    Month 28 (6 | multiply): 83 333
    Month 29 (7 | multiply): 100 000
    Total Created:412 918
    Le problème, on atteins pas 500 000. L'écart est encore plus grand si on change le nombre de mois ou le total voulu.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Total : 500 000
    Total month:20
    ...
    Total Created:334 513
    Quelqu'un aurait une autre idée ?

    Merci.

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Je ne comprends pas la complexité de ton code.

    Le pas c'est 500000 / 12

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php
    $Total = 500000;
    $totalMonth = 30;
    echo 'Total : '.number_format($Total, 0, '.', ' ').PHP_EOL;
    echo 'Total month : '.$totalMonth.PHP_EOL;
    $step = ceil($Total / $totalMonth);
    $amount = 0;
    for ($i = 0; $i < $totalMonth - 1; $i++) {
    		$amount += $step;
    		echo 'Month '.$i. ' : ' . $amount . PHP_EOL;
    }
    echo 'Month '.$i++. ' : ' . $Total . PHP_EOL;
     
    echo 'Total Created : '.number_format($amount, 0, '.', ' ');
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Points : 264
    Points
    264
    Par défaut
    Oui mais le but c'est d'avoir une évolution progressive de chaque mois. L'algo que j'ai codé, le premier mois il y a 694 et le dernier mois 100 000 , c'est progressif.

    Avec ton algo, $step n'évolue pas:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Month 0 : 16667
    Month 1 : 16667
    Month 2 : 16667
    Month 3 : 16667
    Month 4 : 16667
    Month 5 : 16667
    Month 6 : 16667
    Month 7 : 16667
    Month 8 : 16667
    Month 9 : 16667
    Month 10 : 16667
    Month 11 : 16667
     
    Month 23 : 16667
    Month 24 : 16667
    Month 25 : 16667
    Month 26 : 16667
    Month 27 : 16667
    Month 28 : 16667
    Month 29 : 16667
    Total Created : 483 343
    C'est là toute la complexité du problème, avoir une évolution progressive du montant pour chaque mois avec un total prédéfini. A un moment je pensais "ça doit être exponentiel" mais je vois pas trop comment l'utiliser.

  4. #4
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    C'est justement toute la question : comment définir "évolution progressive" ?

    Par exemple : en doublant la somme à chaque fois ? (L'inverse de la dichotomie)
    Ou 1,5 fois ?
    Ou... ?

    Une fois définie la méthode, la seule variable à calculer est la somme de départ.

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Points : 264
    Points
    264
    Par défaut
    Oui je vois, disons que l'on double.

    Disons que l'on a:
    - $Total (200)
    - $totalMonth (6)

    Comment on peut calculer la somme du premier ?

    Par l'exemple avec 3 pour le premier mois:
    3, 6, 12, 24, 48, 96 -> Total 189.

    Donc comment tomber sur 3 avec 200, 6 et 2 (on double) ?

  6. #6
    Invité
    Invité(e)
    Par défaut
    Il faut faire les calcul dans l'autre sens : du dernier au premier, en divisant par 2 à chaque fois.

    Par contre, on arrive à 0 rapidement (alors que tu veux des entiers positifs !)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    <?php
    function progressive_numbers( $total, $nbMonth )
    {
    	echo 'Total : '.number_format($total, 0, '.', ' ') . '<br />';
    	echo 'Total month : '.$nbMonth . '<br />';
    	$array = array();
    	$all = $total;
    	$amount = 0;
    	// Progression par dichotomie
    	for( $i=$nbMonth; $i>=1; $i--) // order inverse
    	{
    		if( $i != 1){
    			$amount = floor($all/2); // on divise par 2
    			$all = $all - $amount;
    		} else {
    			$amount = $total - array_sum($array);
    		}
    		$array[$i] = $amount;
    	}
    	asort($array);
    	foreach( $array as $i => $amount)
    	{
    		echo 'Month '.$i. ' : ' . $amount . '<br />';
    	}
    	echo 'Total Created : '.number_format( array_sum($array), 0, '.', ' '); // somme des valeurs
     
    }
    //------------------
    $total = 500000;
    $nbMonth = 30;
    progressive_numbers( $total, $nbMonth );
    //------------------
    ?>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    Total : 500 000
    Total month : 30
    Month 8 : 0
    Month 9 : 0
    Month 10 : 0
    Month 11 : 0
    Month 6 : 0
    Month 7 : 0
    Month 5 : 0
    Month 3 : 0
    Month 2 : 0
    Month 4 : 0
    Month 12 : 1
    Month 1 : 1
    Month 13 : 2
    Month 14 : 4
    Month 15 : 8
    Month 16 : 15
    Month 17 : 31
    Month 18 : 61
    Month 19 : 122
    Month 20 : 244
    Month 21 : 488
    Month 22 : 977
    Month 23 : 1953
    Month 24 : 3906
    Month 25 : 7812
    Month 26 : 15625
    Month 27 : 31250
    Month 28 : 62500
    Month 29 : 125000
    Month 30 : 250000
    Total Created : 500 000

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Points : 264
    Points
    264
    Par défaut
    On ne peut pas diviser par 2 directement, cela doit dépendre aussi du nombre de mois.

  8. #8
    Invité
    Invité(e)
    Par défaut
    Sauf que tu n'as toujours pas précisé ce que tu entends par "progressif".

    Ca peut être :
    • avec un pas constant -> c'est relativement simple à mettre en place
    • avec un pas variable (en % ? parabolique ?... ?)

    Une valeur mini ? maxi ?
    ...

  9. #9
    Invité
    Invité(e)
    Par défaut
    Un exemple qui fonctionne :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <?php
    function progressive_numbers( $total, $nbMonth )
    {
    	echo 'Total : '.number_format($total, 0, '.', ' ') . '<br />';
    	echo 'Total month : '.$nbMonth . '<br />';
    	$array = array();
    	$all = $total;
    	$moyenne = floor($total/$nbMonth); // valeur moyenne
    	$step = $moyenne * 0.01; // 1% de la valeur moyenne
    	$depart = $moyenne - ($step * $nbMonth/2);
    	$amount = 0;
    	// Progression
    	for( $i=1; $i<=$nbMonth; $i++)
    	{
    		if( $i != $nbMonth){
    			$amount = floor( $depart + $i * $step );
    			$all = $all - $amount;
    		} else {
    			$amount = $total - array_sum($array);
    		}
    		$array[$i] = $amount;
    	}
    	asort($array);
    	foreach( $array as $i => $amount)
    	{
    		echo 'Month '.$i. ' : ' . $amount . '<br />';
    	}
    	echo 'Total Created : '.number_format( array_sum($array), 0, '.', ' '); // somme des valeurs
     
    }
    //------------------
    $total = 500000;
    $nbMonth = 30;
    progressive_numbers( $total, $nbMonth );
    //------------------
    ?>
    Avec 1% :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    Total : 500 000
    Total month : 30
    Month 1 : 14332
    Month 2 : 14499
    Month 3 : 14666
    Month 4 : 14832
    Month 5 : 14999
    Month 6 : 15166
    Month 7 : 15332
    Month 8 : 15499
    Month 9 : 15666
    Month 10 : 15832
    Month 11 : 15999
    Month 12 : 16166
    Month 13 : 16332
    Month 14 : 16499
    Month 15 : 16666
    Month 30 : 16700
    Month 16 : 16832
    Month 17 : 16999
    Month 18 : 17165
    Month 19 : 17332
    Month 20 : 17499
    Month 21 : 17665
    Month 22 : 17832
    Month 23 : 17999
    Month 24 : 18165
    Month 25 : 18332
    Month 26 : 18499
    Month 27 : 18665
    Month 28 : 18832
    Month 29 : 18999
    Total Created : 500 000
    N.B. array_sum($array) donne la somme des valeurs.

    Avec 5% :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    Total : 500 000
    Total month : 30
    Month 1 : 4999
    Month 2 : 5833
    Month 3 : 6666
    Month 4 : 7499
    Month 5 : 8332
    Month 6 : 9166
    Month 7 : 9999
    Month 8 : 10832
    Month 9 : 11666
    Month 10 : 12499
    Month 11 : 13332
    Month 12 : 14166
    Month 13 : 14999
    Month 14 : 15832
    Month 15 : 16666
    Month 30 : 16700
    Month 16 : 17499
    Month 17 : 18332
    Month 18 : 19165
    Month 19 : 19999
    Month 20 : 20832
    Month 21 : 21665
    Month 22 : 22499
    Month 23 : 23332
    Month 24 : 24165
    Month 25 : 24999
    Month 26 : 25832
    Month 27 : 26665
    Month 28 : 27498
    Month 29 : 28332
    Total Created : 500 000

  10. #10
    Membre actif
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Points : 264
    Points
    264
    Par défaut
    Yes, super merci jreaux62.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 28/03/2010, 15h05
  2. Réponses: 3
    Dernier message: 24/11/2008, 14h31
  3. [MySQL] Affichage données sur 12 mois
    Par dunbar dans le forum PHP & Base de données
    Réponses: 11
    Dernier message: 21/11/2008, 23h52
  4. incrémentation auto à partir d'un chiffre donné!
    Par BRUNO71 dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 15/05/2008, 20h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo