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

Python Discussion :

réduction taille code [Python 2.X]


Sujet :

Python

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 33
    Points : 47
    Points
    47
    Par défaut réduction taille code
    reBonjour,

    je suis partie sur programme qui calcule du factorielle. Grâce a ce site : http://villemin.gerard.free.fr/Wwwgv...r/Factsome.htm dans le premier tableau on nous explique que à chaque multiple de 5, le factorielle "gagne" un 0 ( donc multiplier par 10). Ne sachant pas quand est-ce que le programme doit ce finir, j'ai décidé d'enlever les 0 ( pour un gain de mémoire) et par la suite les remettre ( bref c'est une autre chose). Donc voici mon code:

    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
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    import math
     
    def factorielle (n):
     
    	fac = 1
     
    	while n != 0:
     
    		fac *= n
     
    		n -= 1
    		#print (fac)
    	return fac
     
     
    if __name__ == "__main__" :
     
    	print('n=')
    	n=input()
     
    	if n>4 and n<10:
    		a = factorielle(n)
    		b = a/10
    		print (b)
    	elif n>9 and n<15:
    		a = factorielle(n)
    		b = a/100
    		print (b)
    	elif n>14 and n<20:
    		a = factorielle(n)
    		b = a/1000
    		print (b)
    	elif n>19 and n<25:
    		a = factorielle(n)
    		b = a/10000
    		print (b)
    	elif n>24 and n<30:
    		a = factorielle(n)
    		b = a/100000
    		print (b)
    	elif n>29 and n<35:
    		a = factorielle(n)
    		b = a/1000000
    		print (b)
    	elif n>34 and n<40:
    		a = factorielle(n)
    		b = a/10000000
    		print (b)
    	elif n>39 and n<45:
    		a = factorielle(n)
    		b = a/100000000
    		print (b)
    	elif n>44 and n<50:
    		a = factorielle(n)
    		b = a/1000000000
    		print (b)
    	elif n>54 and n<60:
    		a = factorielle(n)
    		b = a/10000000000
    		print (b)
    	elif n>59 and n<65:
    		a = factorielle(n)
    		b = a/100000000000
    		print (b)
    	elif n>64 and n<70:
    		a = factorielle(n)
    		b = a/1000000000000
    		print (b)
    	elif n>69 and n<75:
    		a = factorielle(n)
    		b = a/10000000000000
    		print (b)
    	elif n>74 and n<80:
    		a = factorielle(n)
    		b = a/100000000000000
    		print (b)
    	elif n>79 and n<85:
    		a = factorielle(n)
    		b = a/1000000000000000
    		print (b)
    	elif n>84 and n<90:
    		a = factorielle(n)
    		b = a/10000000000000000
    		print (b)
    	elif n>89 and n<95:
    		a = factorielle(n)
    		b = a/100000000000000000
    		print (b)
    	elif n>94 and n<100:
    		a = factorielle(n)
    		b = a/1000000000000000000
    		print (b)
    Comme on peut le constater, c'est un code bourrin, voir plus ! je ne voie aucun moyen de faire plus court,surtout qu'il y a 3 paramètre à changer à chaque fois !

    auriez-vous des idées?
    PS: le programme fonction tel qu'il est ! tester et approuvé !

  2. #2
    Membre du Club
    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2008
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2008
    Messages : 48
    Points : 65
    Points
    65
    Par défaut
    Bonjour,
    pour avoir le nombre de zéro pour n, tu peux utiliser la division entière par 5 de n. En python on l'écrit //
    Donc tu pourrais écrire ton code

    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
     
    import math
    def factorielle (n):
     
    	fac = 1
     
    	while n != 0:
     
    		fac *= n
     
    		n -= 1
    		#print (fac)
    	return fac
     
     
    if __name__ == "__main__" :
     
    	print('n=')
    	n=input()
    	a = factorielle(n)
    	b = a/pow(10,n//5)
    	print (b)

  3. #3
    Membre averti
    Homme Profil pro
    Développeur en formation
    Inscrit en
    Juillet 2013
    Messages
    300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur en formation
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juillet 2013
    Messages : 300
    Points : 413
    Points
    413
    Par défaut
    Le plus simple (et probablement le plus rapide, comme il est écrit en C) reste la fonction factorial du module math . Son code source est, si je ne me trompe pas, disponible ici à partir de la ligne 1407.

    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
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    static PyObject *
    math_factorial(PyObject *self, PyObject *arg)
    {
        long x;
        int overflow;
        PyObject *result, *odd_part, *two_valuation;
     
        if (PyFloat_Check(arg)) {
            PyObject *lx;
            double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
            if (!(Py_IS_FINITE(dx) && dx == floor(dx))) {
                PyErr_SetString(PyExc_ValueError,
                                "factorial() only accepts integral values");
                return NULL;
            }
            lx = PyLong_FromDouble(dx);
            if (lx == NULL)
                return NULL;
            x = PyLong_AsLongAndOverflow(lx, &overflow);
            Py_DECREF(lx);
        }
        else
            x = PyLong_AsLongAndOverflow(arg, &overflow);
     
        if (x == -1 && PyErr_Occurred()) {
            return NULL;
        }
        else if (overflow == 1) {
            PyErr_Format(PyExc_OverflowError,
                         "factorial() argument should not exceed %ld",
                         LONG_MAX);
            return NULL;
        }
        else if (overflow == -1 || x < 0) {
            PyErr_SetString(PyExc_ValueError,
                            "factorial() not defined for negative values");
            return NULL;
        }
     
        /* use lookup table if x is small */
        if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
            return PyLong_FromUnsignedLong(SmallFactorials[x]);
     
        /* else express in the form odd_part * 2**two_valuation, and compute as
           odd_part << two_valuation. */
        odd_part = factorial_odd_part(x);
        if (odd_part == NULL)
            return NULL;
        two_valuation = PyLong_FromLong(x - count_set_bits(x));
        if (two_valuation == NULL) {
            Py_DECREF(odd_part);
            return NULL;
        }
        result = PyNumber_Lshift(odd_part, two_valuation);
        Py_DECREF(two_valuation);
        Py_DECREF(odd_part);
        return result;
    }
    Bouddha : Tout n'est qu'illusion en ce bas monde.
    Jésus : Tout est amour divin.
    Einstein : Tout est relatif dans cet espace-temps.
    Moi : Tout est binaire sur ce forum.

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 33
    Points : 47
    Points
    47
    Par défaut
    Super ! mer! ça reduit beaucoup mon programme x)

    juste une question, du coup ça peut le faire a l'infinie non ? ( dans la limite des variables)

  5. #5
    Membre du Club
    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2008
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2008
    Messages : 48
    Points : 65
    Points
    65
    Par défaut
    Dans la limite des temps de calcul je ne pense pas que tu ais de limitation sur n sinon.

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

Discussions similaires

  1. Réduction de code
    Par NeMo_O dans le forum Algorithmes et structures de données
    Réponses: 7
    Dernier message: 21/08/2008, 14h38
  2. réduction de code
    Par lefelinherbivore dans le forum ActionScript 1 & ActionScript 2
    Réponses: 1
    Dernier message: 24/04/2008, 21h49
  3. Réduction de code
    Par mouaa dans le forum VBA Access
    Réponses: 13
    Dernier message: 27/12/2007, 14h45
  4. réduction taille fichier
    Par Bibouda dans le forum Autres Logiciels
    Réponses: 3
    Dernier message: 27/10/2005, 17h43
  5. Réponses: 12
    Dernier message: 11/04/2005, 18h31

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