DEV/ControleMachine1/Exo6.c
2022-10-26 16:46:01 +02:00

67 lines
1.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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= 35 + 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;
}