67 lines
1.1 KiB
C
67 lines
1.1 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
/* 1)
|
||
|
||
n = 8
|
||
u0 = 8,
|
||
u1 = 8/2 = 4 car 8 est pair
|
||
u2 = 4/2 = 2
|
||
u3 = 2/2 = 1
|
||
|
||
n = 9
|
||
u0 = 9
|
||
u1 = 9*3+1 = 28 car 9 est impair
|
||
u2 = 28/2 = 14 car 28 est pair
|
||
u3 = 14/2 = 7
|
||
u4 = 7*3+1 = 22
|
||
u5 = 22/2 = 11
|
||
u6 = 11*3+1 = 34
|
||
u7 = 34/2 = 17
|
||
u8 = 17*3+1 = 52
|
||
u9 = 52/2 = 26
|
||
u10 = 26/2 = 13
|
||
u11 = 13*3+1 = 40
|
||
u12 = 40/2 = 20
|
||
u13 = 20/2 = 10
|
||
u14 = 10/2 = 5
|
||
u15= 3∗5 + 1 = 16
|
||
u16= 16/2 = 8
|
||
u17= 8/2 = 4
|
||
u18= 4/2 = 2
|
||
u19= 2/2 = 1 */
|
||
|
||
int main (void){
|
||
int x;
|
||
int i=0;
|
||
int j;
|
||
printf("Veuillez saisir un entier: ");
|
||
scanf("%d", &x);
|
||
printf("u0 = %d\n",x);
|
||
while (1<x){
|
||
if((x%2)==0){ //Permet de savoir si x est pair
|
||
x=x/2;
|
||
i++;
|
||
printf("u%d = %d\n",i,x);
|
||
} else{
|
||
x=3*x+1;
|
||
i++;
|
||
printf("u%d = %d\n",i,x);
|
||
}
|
||
} printf("Il a fallut %d etapes pour obtenir 1.\n",i);
|
||
|
||
for(j=0;j<101;j++){
|
||
x=j;
|
||
i=0;
|
||
while (1<x){
|
||
if((x%2)==0){ //Permet de savoir si x est pair
|
||
x=x/2;
|
||
i++;
|
||
} else{
|
||
x=3*x+1;
|
||
i++;
|
||
}
|
||
} printf("La longueur de la suite pour x=%d est de %d\n",j,i);
|
||
}
|
||
return 0;
|
||
} |