unsigned long long int first( int b , int c){
int h=b;
//int k;
for(int k=b-1;k>c;k--){
b=b*k;
}
int comb=b/factorial(h-c);
return comb;
}
this function return right answers for some cases and wrong answer to others. can anyone help me please is there anything wrong with this function?!
Since int comb is int you are returning int!
It does not matter that you are suppose to return unsigned long long int first. The result of the devision is rounded to int value, precision is lost.
Related
#include<stdio.h>
int fac(int m){
long long int i,s=1;
for(i=1;i<=m;i++){
s*=i;
}
return s;
}
int main(){
int n;
scanf("%d",&n);
printf("%d",fac(n));
}
when I tried to put 13 in n,
the answer was supposed to be 13!, which is 6227020800, but 1932053504 came out.
Your function returns int, but the value it's trying to return is long long int.
Also the printf format %d expects an int, but you should be passing it a long long int. %ld is the format for a long int. I don't know how you handle it with long long int. I don't think you need a long long int anywhere in here anyway.
I need to implement the karatsuba algortihm into a c code for my homework and I did my research and came up with the following code:
long int karatsuba(long int x,long int y)
{
if((x<10)||(y<10)) \\if the numbers have 1 digit, I just multiply them
return x*y;
else
{
long int a, b, c, d, ac, bd, z;
int n=uzunluk(x);
a=floor(x/ust(10, ceil(n/2)));
b=x%ust(10, ceil(n/2));;
c=floor(y/ust(10, ceil(n/2)));
d=y%ust(10, ceil(n/2));;
ac=a*c;
bd=b*d;
z=(a+b)*(c+d)-ac-bd;
long int res=ust(10, 2*ceil(n/2))*ac+ust(10, ceil(n/2))*z+bd;
return res;
}
}
int main(void)
{
printf("%ld", karatsuba(837487, 368498));
return 0;
}
ust(x, n) is the function to get the power of number x:
long int ust(long x, long n)
{
long int res=1;
int i;
for(i=0; i<n; i++)
{
res*=x;
}
return res;
}
And the uzunluk(x) gets the number of digits in the given input:
int uzunluk(long int x)
{
int lx;
while(x>0)
{
x/=10;
lx+=1;
}
return lx;
}
the problem is this code prints nothing :D
I would be glad if someone could spot the mistake I made.
So it comes out the problem was 7 digits numbers' multiplication does not result proper under the long integer identification. As I changed it to long long int, it started working properly. Thank you all for your help
My code for finding 10th decimal digit of square root of 2.
#include <stdio.h>
unsigned long long int power(int a, int b);
unsigned long long int root(int a);
int main()
{
int n;
n=10;
printf("%llu \n",root(n));
return 0;
}
unsigned long long int power(int a, int b)
{
int i;
unsigned long long int m=1;
for (i=1;i<=b;i++)
{
m*=a;
}
return m;
}
unsigned long long int root(int a)
{
unsigned long long int c=1;
int counter=1;
while(counter<=a)
{
c*=10;
while(power(c,2)<=2*power(10,2*counter))
{
c++;
}
c-=1;
counter++;
}
return c;
}
I have tried the same algorithm in python. It can find the 10th decimal digit of $sqrt{2}$ immediately.
However, while doing C, I have waited for 10 mins but without a result.
Python handles big numbers for you. [1]
Although, as you say that you are getting the answer "immediately", your algorithm in python is not probably the same as the one you used in C.
#bruno's answer already explains why you are not getting the expected results in C.
[1] Handling very large numbers in Python
Exceed the range that the data can represent. when counter is equal to 10,2*power(10,2*counter) exceeds the range that unsigned long long int can represent. Python supports large number calculations, unlimited digits
you have overflow(s)
when counter values 10 you try to compute power(10,20) but even long long on 64 bits are not enough large, so you loop in
while(power(c,2)<=2*power(10,2*counter)){
c++;
}
for a long time (may be without ending)
Having long long on 64 bits allows to compute the result for n valuing up to 9
I write a algorithm to get the sum of float points number, and this algorithm works perfect for integer but when I applied to float points, the sum I got is a negative number. however my array of float points does only have positive float points number. here I post my code, thank you for checking it.
static unsigned int do_heap_sum(double ary[], unsigned int n,
double *result)
{
unsigned int j;
int counter=0;
double sum=0;
double min;
while(n>counter){
make_heap(ary,n);
min=ary[0];
sum=sum+min;
for(j=1;j<n;j++){
ary[j-1]=ary[j];
}
counter=counter+1;
}
*result=sum;
//exit(EXIT_FAILURE);
return 0;
}
in this code, I use the make_heap to find the min. do you think this has problem? (this make_heap works perfectly with integers extracting the min of array).
thanks again.
In your code, you want to calculate sum of the heap which stored in double[], so make_heap is superfluous, a simple loop is enough.
Make sure double is big enough to save the sum.
For ary[j-1] = ary[j] in for(j=1;j<n;j++), why not just swap ary[0] and ary[n - counter - 1] then adjust the heap in next loop, it's much cheaper.
My wild guess is that you used an integer version of make_heap:
void make_heap(int *ary,size_t n);
instead of a double precision float version:
void make_double_heap(double *ary,size_t n);
Otherwise, I notice that you sort a heap of size n, n times: that's too much work. What happens when some elements of ary are duplicated ?
You could rewrite the loop:
static unsigned int do_heap_sum(double ary[], unsigned int n,
double *result)
{
unsigned int counter=0;
double sum=0;
double min;
while(n>counter){
make_double_heap(ary+counter,n-counter);
min=ary[counter];
sum=sum+min;
counter=counter+1;
}
*result=sum;
return 0;
}
Here is my code, for some reason I have to use unsigned long. The gdb tells me that I have
seg fault. Can one can help me? I could not find it by myself. The most interesting thing is that if I change the type to int from unsigned long, there is no seg fault.
Code is here:
#include <stdio.h>
int counting_Sort (unsigned long ary[], unsigned long array_size,unsigned long max){
unsigned long counting[max+1];
unsigned long j;
for(j=0;j<max+1;j++){
counting[j]=0;//initize to zero
}
unsigned long i;
for(i=0;i<array_size;i++){
counting[ary[i]]++;
}
unsigned long q;
for(q=1;q<max+1;q++){
counting[q]=counting[q-1]+counting[q];
}
for(q=0;q<max+1;q++){
counting[q]=counting[q]-1;
}
unsigned long outputAry[array_size];
unsigned long d;
for(d=(array_size-1); d>=0;d--){
outputAry[counting[ary[d]]]=ary[d];// SEG FAULT IS HERE
counting[ary[d]]--;//AND HERE
}
unsigned long m;
//for(m=0; m<array_size;m++){
// printf("%lu\n",outputAry[m]);
// }
return 0;
}
int main(){
unsigned long array[7]={2,6,4,0,1,7,9};
printf("before sorting the order is: \n");
unsigned long i;
for(i=0;i<7;i++){
printf("%lu\n",array[i]);
}
printf("after sorting, the new order is: \n");
counting_Sort(array,7,9);
getchar();
return 0;
}
You've found the place, just not the reason.
unsigned long d;
for(d=(array_size-1); d>=0;d--){
d is an unsigned integer, which means d>=0 is always true. The loop never ends, and that's the reason of segmentation fault.
One way is to change d to a singed type:
int d;
But if that's not what you want, change the for loop to:
for (d = 0; d <= array_size - 1; d++){