From 97470838b2314a946f5b3d5f5ff36e069342621a Mon Sep 17 00:00:00 2001 From: Simon Saye Babu Date: Wed, 14 Dec 2022 14:01:46 +0100 Subject: [PATCH] recur --- DEV1.1/TP14/power.c | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 DEV1.1/TP14/power.c diff --git a/DEV1.1/TP14/power.c b/DEV1.1/TP14/power.c new file mode 100644 index 0000000..0f46299 --- /dev/null +++ b/DEV1.1/TP14/power.c @@ -0,0 +1,101 @@ +#include +#include + +int powRec(int x, int n) +{ + if (n==0) + { + return 1; + } + else{ + x=x*powRec(x,n-1); + return x; + } +} + + +int powImp(int x, int n) +{ + if (n==0) + { + return 1; + }else + { + for (int i = 1; i < n; ++i) + { + x=x*x; + } + return x; + } +} + +void mystere(int n) +{ + if (n==0) + { + /* rien */ + }else + { + mystere(n/2); + printf("%d",n%2); + } +} + +void mystereImp(int n) +{ + int tab[32]; + int i; + if (n==0) + { + /* rien */ + }else + { + for (i = 0; n!=0 ; ++i) + { + tab[i]=n%2; + n=n/2; + } + for (int j = i-1; j > -1 ; --j) + { + printf("%d",tab[j]); + } + } +} + + +int iteration=-1; + +void syracuse2(int n) +{ + iteration++; + if (n==1) + { + printf("u%d = %d\n",iteration,1); + } + else{if ((n%2)==0) + { + printf("u%d = %d/2 = %d\n",iteration,n,n/2); + syracuse2(n/2); + }else + { + printf("u%d = 3*%d+1 = %d\n",iteration,n,(3*n)+1); + syracuse2((3*n)+1); + }} +} +void syracuse(int n) +{ + iteration=-1; + syracuse2(n); +} + + +int main(int argc, char const *argv[]) +{ + /*mystere(5646); + printf("\n"); + mystereImp(5646);*/ + syracuse(5); + syracuse(13); + + return 0; +} \ No newline at end of file