Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes | ||
teaching:progappchim:suite_de_fibonacci-3 [2013/10/24 08:43] – créée villersd | teaching:progappchim:suite_de_fibonacci-3 [2015/02/26 12:39] – villersd | ||
---|---|---|---|
Ligne 21: | Ligne 21: | ||
</ | </ | ||
- | Le rôle de la structure conditionnelle **< | + | Le rôle de la structure conditionnelle **< |
+ | |||
+ | Voici une proposition complète : | ||
+ | <sxh python; title : fibonacci05_fonction.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Calculs des premiers éléments de la suite de Fibonacci. | ||
+ | Référence : http:// | ||
+ | """ | ||
+ | def fibonacci_item(n): | ||
+ | """ | ||
+ | Renvoie l' | ||
+ | """ | ||
+ | a, b = 0, 1 | ||
+ | if n==0: | ||
+ | return a | ||
+ | elif n==1: | ||
+ | return b | ||
+ | for i in range(1, | ||
+ | a, b = b, a + b | ||
+ | return b | ||
+ | |||
+ | if __name__ == ' | ||
+ | i=input(" | ||
+ | print (" | ||
+ | print fibonacci_item(i) | ||
+ | print (' | ||
+ | for j in range(10): | ||
+ | print j, | ||
+ | </ | ||
+ | |||
+ | On peut compléter les fonctionnalités | ||
+ | |||
+ | Voici ce que cela donne : | ||
+ | <sxh python; title : fibonacci06_fonctions.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Calculs des premiers éléments de la suite de Fibonacci. | ||
+ | Référence : http:// | ||
+ | """ | ||
+ | def fibonacci_item(n): | ||
+ | """ | ||
+ | Renvoie l' | ||
+ | """ | ||
+ | a, b = 0, 1 | ||
+ | if n==0: | ||
+ | return a | ||
+ | elif n==1: | ||
+ | return b | ||
+ | for i in range(1, | ||
+ | a, b = b, a + b | ||
+ | return b | ||
+ | |||
+ | def fibonacci_list(n): | ||
+ | """ | ||
+ | Renvoie la liste des éléments de la suite de Fibonacci jusqu' | ||
+ | """ | ||
+ | a,b,ans = 0,1,[0,1] | ||
+ | if n==0: | ||
+ | return [0] | ||
+ | for i in range(1, | ||
+ | a, b = b, a + b | ||
+ | ans.append(b) | ||
+ | return ans | ||
+ | |||
+ | def fibonacci_list_from_items(n): | ||
+ | # construit la liste à partir de la fonction donnant un élément | ||
+ | ans = [] | ||
+ | for i in range(n+1): | ||
+ | ans.append(fibonacci_item(i)) | ||
+ | return ans | ||
+ | |||
+ | def fibonacci_item_from_list(n): | ||
+ | # renvoie l' | ||
+ | return fibonacci_list(n)[n] | ||
+ | |||
+ | if __name__ == ' | ||
+ | i=input(" | ||
+ | print (" | ||
+ | print fibonacci_item(i) | ||
+ | print (' | ||
+ | for j in range(10): | ||
+ | print j, | ||
+ | |||
+ | print ('Avec fibonacci_item_from_list : ') | ||
+ | for j in range(10): | ||
+ | print j, | ||
+ | |||
+ | |||
+ | print (" | ||
+ | print fibonacci_list(i) | ||
+ | print (' | ||
+ | for j in range(10): | ||
+ | print j, | ||
+ | |||
+ | print "Avec fibonacci_list_from_items" | ||
+ | for j in range(10): | ||
+ | print j, | ||
+ | </sxh> | ||
+ | |||
+ | Des fonctions | ||
+ | |||
+ | <sxh python; title : fibonacci07_fonction_recursive.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Calculs des premiers éléments de la suite de Fibonacci. | ||
+ | Référence : http:// | ||
+ | Application de la définition | ||
+ | """ | ||
+ | def fibonacci_item_recursive(n): | ||
+ | """ | ||
+ | Renvoie l' | ||
+ | """ | ||
+ | ... (?) | ||
+ | return fibonacci_item_recursive(n-1)+fibonacci_item_recursive(n-2) | ||
+ | |||
+ | if __name__ == ' | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | [[suite_de_fibonacci-4|Pour la suite, cliquez ici !]] |