
December 3rd, 2012, 05:33 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 5 h 39 m 29 sec
Reputation Power: 0
|
|
|
Help in c
hi, i need to right a prog that calculates sums of geometric series. with the use of function building and declaring...
user enter a number, and it finds all the series that sums up to that number.
example:
31
31 = 1 + 2 + 4 + 8 + 16
31 = 1 + 5 + 25
31 = 1 + 30
no use in math lib.
i've built a working code, but it is only good until 1251.
please help...
:
Code:
int power(int x, int y) /* function that calculates a power number */
{
int sum;
for (sum=1; y>0; y--)
sum*=x;
return sum;
}
int lg(int base, int x) /*function that finds the power number */
{
int counter=0, temp=1;
while (x>temp)
{
temp=temp*base;
counter++;
}
return counter;
}
void geometric()
{
int i=0,j,q=2,n=0,a1=0,num,sum, temp;
int solution[3][10000];
for (j=0;j<10000; j++)
{
solution[0][j]=0;
solution[1][j]=0;
solution[2][j]=0;
}
/* first, get the num for the sum of the serie */
num=get_int();
/*now, find a1 and a sutiable q and n from a1=1, q=2*/
for (q=2; q<num; q++)
for (a1=1; a1<num; a1++)
{
temp=(int)(((num*(q-1))/a1)+1);
n=lg(q,temp);
sum=(int)((a1*(power(q,n)-1)/(q-1)));
if (num==sum)
{
printf("G");
solution[0][i]=n; /* the output is this array */
solution[1][i]=a1; /* which contains in each */
solution[2][i]=q; /* column n, a1 and q for */
i++; /* the sum */
}
}
for (j=0; solution[0][j]!=0; j++)
printf("a1=%d q=%d n=%d\n",solution[1][j],solution[2][j],solution[0][j]);
}
|