initial import
[staff/goc4/2018bti7061.git] / c-7-compilation / examples / autotools / myLib.c
CommitLineData
462e967d
CG
1#include <stdio.h>
2
3#include "myLib.h"
4
5/* Compute the exponential of a value
6Input : val a long integer
7Return : exponential(x) (i.e. e^x)
8*/
9double exponential(long val){
10 double res= 1;
11 long i;
12 for(i=1; i <= val;i++){
13 res *= EULER;
14 }
15 return res;
16}
17
18/* Compute the factorial of a value
19Input : val a long integer
20Return : factorial(x) (i.e. x!)
21*/
22long factorial(long val){
23 long res= 1;
24 long i;
25 for(i=1; i <= val;i++){
26 res *= i;
27 }
28 return res;
29}
30
31
32/* Compute the protected integer decimal logarithm of val
33Input: val a long integer
34Return : an integer for decimal logarithm of val
35or 0 if val is 0
36*/
37long logarithm(long val){
38 long res=0;
39 if(val<10){
40 return 0;
41 }
42 while(val>=10){
43 val /=10;
44 res++;
45 }
46 return res;
47}
48
49
50/* Function square that returns x^2
51Input: a long integer x
52Ouput: a long integer x*x
53*/
54long square(long x){
55 return x*x;
56}
57
58
59/* Function gcd computes the greater common divisor of two numbers
60Input: two long integers numbers x and y
61Output: the greatest common divisor of x and y
62*/
63long gcd(long number1, long number2){
64
65 long divisor,gcd;
66
67 if(number1 == 0 || number2 == 0){
68 return 0;
69 }
70
71 if(number1 < 0 || number2<0){
72 printf("GCD formula only for positive numbers\n");
73 return;
74 }
75
76 divisor = 2;
77 gcd = 1;
78 //printf("1");
79 while(number1!=1 || number2!=1){
80 /* Case where divisor devises number */
81 if(((number1 % divisor) == 0) && ((number2 % divisor)==0)){
82 number1 /= divisor;
83 number2 /= divisor;
84 gcd *= divisor;
85
86 //printf(" * %ld",divisor);
87 }
88 else{
89 if((number1 % divisor)==0){
90 number1 /= divisor;
91 }
92 else if((number2 % divisor) == 0){
93 number2 /= divisor;
94 }
95 else
96 divisor++;
97 }
98
99 }
100 return gcd;
101
102}