Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| teaching:progappchim:factorielle-3 [2015/02/17 10:07] – créée villersd | teaching:progappchim:factorielle-3 [2017/02/24 09:28] (Version actuelle) – villersd | ||
|---|---|---|---|
| Ligne 2: | Ligne 2: | ||
| Voici une version avec la fonction factorielle() | Voici une version avec la fonction factorielle() | ||
| - | <sxh python; title : factorielle04-fonction_1.py> | + | <code python factorielle04-fonction_1.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 10: | Ligne 10: | ||
| """ | """ | ||
| def factorielle(arg_n): | def factorielle(arg_n): | ||
| - | | + | |
| - | reponse=1 | + | |
| - | i=1 # on va commencer par 1 | + | """ |
| - | while i <= arg_n: | + | reponse = 1 |
| - | reponse = reponse*i | + | i = 1 |
| - | i=i+1 # | + | while i <= arg_n: |
| + | reponse = reponse * i # | ||
| + | i = i + 1 | ||
| return reponse | return reponse | ||
| # on demande le nombre : | # on demande le nombre : | ||
| - | print " | + | print(" |
| - | chainelue=raw_input("Que vaut n ? ") | + | chainelue=input("Que vaut n ? ") |
| - | n= int(chainelue) | + | n = int(chainelue) |
| - | print n | + | print(n) |
| # on affiche la réponse | # on affiche la réponse | ||
| - | print "La factorielle vaut ", | + | print("La factorielle vaut ", |
| - | </sxh> | + | </code> |
| + | |||
| + | On aurait pu utiliser une autre structure de répétition que " | ||
| + | |||
| + | <code python> | ||
| + | def factorielle2(arg_n): | ||
| + | """ | ||
| + | structure de répétition pour appliquer la définition de la factorielle | ||
| + | """ | ||
| + | reponse = 1 # la réponse sera dans la variable reponse | ||
| + | for i in range(1, arg_n + 1): # répétition " | ||
| + | reponse = reponse * i # actualisation de reponse | ||
| + | return reponse | ||
| + | </ | ||
| + | |||
| + | |||
| + | Il est aussi possible d' | ||
| + | |||
| + | <code python> | ||
| + | def factorielle3(arg_n): | ||
| + | """ | ||
| + | structure de répétition pour appliquer la définition de la factorielle | ||
| + | """ | ||
| + | reponse = 1 # la réponse sera dans la variable reponse | ||
| + | while arg_n > 1: # répétition " | ||
| + | reponse = reponse * arg_n # actualisation de reponse | ||
| + | arg_n = arg_n - 1 # décrémenter arg_n | ||
| + | return reponse | ||
| + | </ | ||
| + | |||
| + | La factorielle étant une fonction courante en mathématique, | ||
| + | |||
| + | Il existe une autre approche de la programmation de la factorielle, | ||
| + | |||
| + | <code python> | ||
| + | def factorielle4(arg_n): | ||
| + | """ | ||
| + | écriture récursive pour appliquer la définition de la factorielle | ||
| + | """ | ||
| + | if arg_n == 0: | ||
| + | return 1 | ||
| + | else: | ||
| + | return factorielle4(arg_n - 1) * arg_n | ||
| + | </ | ||
| + | |||
| + | <note tip> | ||
| + | |||
| + | Pour des travaux additionnels sur le sujet, allez [[factorielle-4|à la page suivante !]] | ||
| - | FIXME : à suivre... | ||