yhi/tp02: add architecture and all files .[cho]
This commit is contained in:
commit
e721285083
|
|
@ -0,0 +1,4 @@
|
|||
Yanis
|
||||
Hermassi
|
||||
yanis.hermassi
|
||||
yanis.hermassi@epita.fr
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef DIGIT_COUNT_H
|
||||
#define DIGIT_COUNT_H
|
||||
|
||||
unsigned char digit_count(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef DIVISOR_SUM_H
|
||||
#define DIVISOR_SUM_H
|
||||
|
||||
unsigned long divisor_sum(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef FACTO_H
|
||||
#define FACTO_H
|
||||
|
||||
unsigned long facto(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef FIBO_H
|
||||
#define FIBO_H
|
||||
|
||||
unsigned long fibo(unsigned long n);
|
||||
|
||||
#endif
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef ISQRT_H
|
||||
#define ISQRT_H
|
||||
|
||||
unsigned long isqrt(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef DIVISOR_SUM_H
|
||||
#define DIVISOR_SUM_H
|
||||
|
||||
unsigned long divisor_sum(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef IS_PERFECT_NUMBER_H
|
||||
#define IS_PERFECT_NUMBER_H
|
||||
|
||||
int is_perfect_number(unsigned long n);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
Binary file not shown.
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include "power_of_two.h"
|
||||
|
||||
unsigned long power_of_two(unsigned char n)
|
||||
{
|
||||
return 1ul << n;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef POWER_OF_TWO_H
|
||||
#define POWER_OF_TWO_H
|
||||
|
||||
unsigned long power_of_two(unsigned char n);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue