yhi/tp02: add architecture and all files .[cho]

This commit is contained in:
Yanis Hermassi 2020-10-12 14:29:24 +02:00
commit e721285083
32 changed files with 266 additions and 0 deletions

4
AUTHORS Normal file
View File

@ -0,0 +1,4 @@
Yanis
Hermassi
yanis.hermassi
yanis.hermassi@epita.fr

13
digit_count/digit_count.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include "digit_count.h"
unsigned char digit_count(unsigned long n)
{
unsigned char i = 0;
while(n>0)
{
i+=1;
n = n/10;
}
return i;
}

View File

@ -0,0 +1,6 @@
#ifndef DIGIT_COUNT_H
#define DIGIT_COUNT_H
unsigned char digit_count(unsigned long n);
#endif

BIN
digit_count/main Executable file

Binary file not shown.

17
digit_count/main.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include "digit_count.h"
unsigned long power_of_two(unsigned char n)
{
return 1ul << n;
}
int main()
{
unsigned long n = 63 ;
for(unsigned long i = 0; i <= n; i+=1)
{
printf("digit_count(%lu) = %u\n",power_of_two(i),digit_count(power_of_two(i)));
}
return 0;
}

33
divisor_sum/divisor_sum.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include "divisor_sum.h"
unsigned long isqrt(unsigned long n)
{
unsigned long r = n;
while (r*r > n)
{
r = r + n/r;
r = r/2;
}
return r;
}
unsigned long divisor_sum(unsigned long n)
{
if(n == 1)
{
return 0;
}
if(n<3)
return 1;
unsigned long tmp = 1;
for(unsigned long i = 2; i*i <= n; i++)
{
if(n%i == 0)
tmp += i +n/i;
if(i*i == n)
tmp-=i;
}
return tmp;
}

View File

@ -0,0 +1,6 @@
#ifndef DIVISOR_SUM_H
#define DIVISOR_SUM_H
unsigned long divisor_sum(unsigned long n);
#endif

BIN
divisor_sum/divisor_sum.o Normal file

Binary file not shown.

BIN
divisor_sum/main Executable file

Binary file not shown.

22
divisor_sum/main.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include "divisor_sum.h"
#include "../isqrt/isqrt.h"
int main(int argc, char** argv)
{
if(argc != 2)
{
errx(1, "Error");
return 1;
}
unsigned long param = strtoul(argv[1],NULL, 10);
if(param == 0)
{
errx(1,"Error");
return 1;
}
printf("divisor_sum(%lu) = %lu", param,divisor_sum(param));
return 0;
}

12
facto/facto.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "facto.h"
unsigned long facto(unsigned long n)
{
unsigned long t = 1;
for(unsigned long i = 2; i <= n; i++)
{
t = i * t;
}
return t;
}

6
facto/facto.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef FACTO_H
#define FACTO_H
unsigned long facto(unsigned long n);
#endif

BIN
facto/main Executable file

Binary file not shown.

12
facto/main.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "facto.h"
int main()
{
unsigned long n = 20;
for(unsigned long i = 1; i <= n; i++)
{
printf("facto(%lu) = %lu\n",i,facto(i));
}
return 0;
}

17
fibo/fibo.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include "fibo.h"
unsigned long fibo(unsigned long n)
{
unsigned long first = 0;
unsigned long second = 1;
unsigned long t = 0;
while(n>0)
{
n--;
t = first + second;
first = second;
second = t;
}
return first;
}

6
fibo/fibo.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef FIBO_H
#define FIBO_H
unsigned long fibo(unsigned long n);
#endif

BIN
fibo/main Executable file

Binary file not shown.

18
fibo/main.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include "fibo.h"
int main()
{
unsigned long n = 90;
for(unsigned long i = 0; i<=10; i++ )
{
printf("fibo(%lu) = %lu\n",i,fibo(i));
}
printf("...\n");
for(unsigned long i = 81; i<= n; i++)
{
printf("fibo(%lu) = %lu\n",i,fibo(i));
}
return 0;
}

13
isqrt/isqrt.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include "isqrt.h"
unsigned long isqrt(unsigned long n)
{
unsigned long r = n;
while (r*r > n)
{
r = r + n/r;
r = r/2;
}
return r;
}

6
isqrt/isqrt.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef ISQRT_H
#define ISQRT_H
unsigned long isqrt(unsigned long n);
#endif

BIN
isqrt/main Executable file

Binary file not shown.

12
isqrt/main.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "isqrt.h"
int main()
{
unsigned long n = 200;
for(unsigned long i = 0; i <= n; i+=8)
{
printf("isqrt(%lu) = %lu\n",i,isqrt(i));
}
return 0;
}

View File

@ -0,0 +1,6 @@
#ifndef DIVISOR_SUM_H
#define DIVISOR_SUM_H
unsigned long divisor_sum(unsigned long n);
#endif

Binary file not shown.

View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include "is_perfect_number.h"
#include "divisor_sum.h"
int is_perfect_number(unsigned long n)
{
if(divisor_sum(n) == n)
return n;
return 0;
}

View File

@ -0,0 +1,6 @@
#ifndef IS_PERFECT_NUMBER_H
#define IS_PERFECT_NUMBER_H
int is_perfect_number(unsigned long n);
#endif

BIN
perfect_numbers/main Executable file

Binary file not shown.

15
perfect_numbers/main.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include "is_perfect_number.h"
#include "divisor_sum.h"
int main()
{
unsigned long n = 8128;
for(unsigned long i = 1; i<=n; i++)
{
int t = is_perfect_number(i);
if(t!=0)
printf("%i\n",is_perfect_number(i));
}
return 0;
}

BIN
power_of_two/main Executable file

Binary file not shown.

12
power_of_two/main.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "power_of_two.h"
int main()
{
unsigned char n = 63;
for(unsigned char i = 0; i <= n; i++)
{
printf("powerof_two(%u) = %lu\n",i,power_of_two(i));
}
return 0;
}

View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include "power_of_two.h"
unsigned long power_of_two(unsigned char n)
{
return 1ul << n;
}

View File

@ -0,0 +1,6 @@
#ifndef POWER_OF_TWO_H
#define POWER_OF_TWO_H
unsigned long power_of_two(unsigned char n);
#endif