Travail à Faire:
La suite de Syracuse repose sur un principe simple. Prenez un nombre au hasard:
Renouvellez cette opération plusieurs fois. Après suffisamment d'itérations, vous devriez finir par tomber sur la valeur 1. Par exemple, à partir de 17, on trouve la suite de valeurs: 52 26 13 40 20 10 5 16 8 4 2 1.
1 2 3 4 5 6 7 8 9 10 |
1: 0 iterations. 2: 1 iterations. 3: 7 iterations. 4: 2 iterations. 5: 5 iterations. 6: 8 iterations. 7: 16 iterations. 8: 3 iterations. 9: 19 iterations. 10: 6 iterations. |
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 |
Questions 1 et 2 #include using namespace std; int main(int argc, char **argv) { int n; int i = 0; cout "Entrer le premier nombre de la suite de Syracuse: "; cin >> n; while (n > 1) { if (n % 2 == 0) n /= 2; else n = n * 3 + 1; i++; cout i ": " n endl; } cout "Suite terminee en " i " iterations." endl; return 0; } Question 3 #include using namespace std; int main(int argc, char **argv) { for (int j=1; j10; j++) { int i = 0; int n = j; while (n > 1) { if (n % 2 == 0) n /= 2; else n = n * 3 + 1; i++; } cout n ": " i " iterations." endl; } return 0; } |