http://formatmysourcecode.blogspot.tw/
目前分類:my_c_lib (7)
- Mar 29 Sat 2014 18:08
format my source code to html
- Mar 29 Sat 2014 18:05
relative prime (互質)
int hcf(int a , int h)
{
int temp;
while(1)
{
temp = a % h;
if(temp == 0)
{
return h;
}
a = h;
h = temp;
}
}
int relativePrimes(int* num , int n)
{
int count = 0;
for(int i = 0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
int a = *(num + i);
int b = *(num + j);
int gcd = hcf(a , b);
if(gcd == 1)
{
count++;
}
}
}
return count;
}
- Mar 29 Sat 2014 18:04
gcd - greatest common divisor
int hcf(int a , int h)
{
int temp;
while(1)
{
temp = a % h;
if(temp == 0)
{
return h;
}
a = h;
h = temp;
}
}
- Mar 29 Sat 2014 18:03
combination by pascal formula
int combination_pascal(int n , int r)
{
if(r == 0 || n == r)
{
return 1;
}
return combination_pascal(n - 1 , r) + combination_pascal(n - 1 , r - 1);
}
- Mar 29 Sat 2014 18:02
combination
int factor(int n)
{
int ret = 1;
for(int i = 2 ; i <= n ; i++ )
{
ret = ret * i;
}
return ret;
}
int combination(int n , int r)
{
int ret = factor(n) / (factor(r) * factor(n-r));
return ret;
}
- Mar 29 Sat 2014 18:01
scan n numbers into array
int* numArray(int n)
{
int* num = (int*)malloc(n * sizeof(int));
for(int i = 0 ; i < n ; i++)
{
scanf("%d\n" , num + i);
}
return num;
}
- Mar 29 Sat 2014 17:32
factor
int factor(int n)
{
int ret = 1;
for(int i = 2 ; i <= n ; i++ )
{
ret = ret * i;
}
return ret;
}