tp1
This commit is contained in:
parent
fce19a8c8a
commit
d20f8c8ed5
3
.clang-format
Normal file
3
.clang-format
Normal file
@ -0,0 +1,3 @@
|
||||
---
|
||||
IndentWidth: 4
|
||||
---
|
39
tp1/exo1.c
Normal file
39
tp1/exo1.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int f_iter(int a, uint b) {
|
||||
int acc = 1;
|
||||
while (b > 0) {
|
||||
acc = acc * a;
|
||||
b = b - 1;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
int rec(int acc, int a, uint b) {
|
||||
if (b == 0) {
|
||||
return acc;
|
||||
} else {
|
||||
return rec(acc * a, a, b - 1);
|
||||
}
|
||||
}
|
||||
int f_rec(int a, uint b) { return rec(1, a, b); }
|
||||
|
||||
int f_exp(int a, uint b) {
|
||||
if (b == 1) {
|
||||
return a;
|
||||
} else if (b % 2 == 0) {
|
||||
return f_exp(a * a, b / 2);
|
||||
} else {
|
||||
return a * f_exp(a * a, (b - 1) / 2);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a = 6;
|
||||
uint b = 7;
|
||||
printf("Méthode iterative : %d^%d = %d\n", a, b, f_iter(a, b));
|
||||
printf("Méthode recursive : %d^%d = %d\n", a, b, f_rec(a, b));
|
||||
printf("Méthode d'exponentation rapide : %d^%d = %d\n", a, b, f_exp(a, b));
|
||||
exit(0);
|
||||
}
|
18
tp1/exo2.c
Normal file
18
tp1/exo2.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint rec(uint acc, uint x) {
|
||||
if (x < 10) {
|
||||
return acc + x;
|
||||
} else {
|
||||
return rec(acc + (x % 10), x / 10);
|
||||
}
|
||||
}
|
||||
|
||||
int f(uint x) { return rec(0, x); }
|
||||
|
||||
int main() {
|
||||
int x = 3459023;
|
||||
printf("Somme des chiffres de %d : %d", x, f(x));
|
||||
exit(0);
|
||||
}
|
29
tp1/exo3.c
Normal file
29
tp1/exo3.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int pow(uint a, uint b) {
|
||||
if (b == 1) {
|
||||
return a;
|
||||
} else if (b % 2 == 0) {
|
||||
return pow(a * a, b / 2);
|
||||
} else {
|
||||
return a * pow(a * a, (b - 1) / 2);
|
||||
}
|
||||
}
|
||||
|
||||
int f(uint n, uint k) {
|
||||
uint acc = 0;
|
||||
while (n > 0) {
|
||||
acc += pow(n, k);
|
||||
n--;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
int main() {
|
||||
uint n = 7;
|
||||
uint k = 3;
|
||||
printf("Somme des puissances %d-ièmes des entiers de 1 à %d: %d", k, n,
|
||||
f(n, k));
|
||||
exit(0);
|
||||
}
|
18
tp1/exo4.c
Normal file
18
tp1/exo4.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint rec(uint acc, uint x) {
|
||||
if (x == 0) {
|
||||
return acc;
|
||||
} else {
|
||||
return rec(acc + 1, x / 2);
|
||||
}
|
||||
}
|
||||
|
||||
uint f(uint x) { return rec(0, x); }
|
||||
|
||||
int main() {
|
||||
uint x = 8;
|
||||
printf("Nombre de chiffre de %d en binaire: %d", x, f(x));
|
||||
exit(0);
|
||||
}
|
17
tp1/exo5.c
Normal file
17
tp1/exo5.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void f(uint x) {
|
||||
if (x > 1) {
|
||||
f(x / 2);
|
||||
}
|
||||
printf("%d", x % 2);
|
||||
}
|
||||
|
||||
int main() {
|
||||
uint x = 4329;
|
||||
printf("%d s'écrit binaire : ", x);
|
||||
f(x);
|
||||
printf("\n");
|
||||
exit(0);
|
||||
}
|
26
tp1/exo6.c
Normal file
26
tp1/exo6.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void rec(uint a, uint b, uint x) {
|
||||
uint guess = (a + b) / 2;
|
||||
printf("Ordinateur : %d\n", guess);
|
||||
if (guess == x) {
|
||||
printf("Arbitre : C'est bon !\n");
|
||||
} else if (guess < x) {
|
||||
printf("Arbitre : C'est plus !\n");
|
||||
return rec(guess, b, x);
|
||||
} else {
|
||||
printf("Arbitre : C'est moins !\n");
|
||||
return rec(a, guess, x);
|
||||
}
|
||||
}
|
||||
|
||||
void f(uint n, uint x) { return rec(0, n, x); }
|
||||
|
||||
int main() {
|
||||
uint x = 4329;
|
||||
uint n = 32413524;
|
||||
printf("x = %d et n = %d\n", x, n);
|
||||
f(n, x);
|
||||
exit(0);
|
||||
}
|
17
tp1/exo7.c
Normal file
17
tp1/exo7.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int f(int a, int b) {
|
||||
int i = 0;
|
||||
while (!((a % i == 0) && (b % i == 0))) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a = 9;
|
||||
int b = 73;
|
||||
printf("ppcm(%d, %d) = %d\n", a, b, f(a, b));
|
||||
exit(0);
|
||||
}
|
11
tp1/main.c
Normal file
11
tp1/main.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int x = 612657948;
|
||||
if (x % 3 == 0)
|
||||
printf("%d est multiple de trois.\n", x);
|
||||
else
|
||||
printf("%d n’est pas multiple de trois, le reste est %d.\n", x, x % 3);
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user