בעיית c מוזרה...
התרגיל אומר לכתוב פונ' שמקבלת מספר, ומחזירה את כל הטורים ההנדסיים שסכומם המספר.
מה שכתבתי עובד אש. אש. אבל עד הערך 1251. מ-1252 נכנס ללולאה אינסופית..
אשמח להסבר...:
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=3,a1=5,num,sum;
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/(q+1); a1++)
{
n=lg(q,(int)num/a1*(q-1)+1);
if (n>num/2) break;
sum=(int)((a1*(power(q,n)-1)/(q-1)));
if (num==sum)
{
printf("G");
solution[0]=n; /* the output is this array */
solution[1]=a1; /* which contains in each */
solution[2]=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]);
}
התרגיל אומר לכתוב פונ' שמקבלת מספר, ומחזירה את כל הטורים ההנדסיים שסכומם המספר.
מה שכתבתי עובד אש. אש. אבל עד הערך 1251. מ-1252 נכנס ללולאה אינסופית..
אשמח להסבר...:
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=3,a1=5,num,sum;
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/(q+1); a1++)
{
n=lg(q,(int)num/a1*(q-1)+1);
if (n>num/2) break;
sum=(int)((a1*(power(q,n)-1)/(q-1)));
if (num==sum)
{
printf("G");
solution[0]=n; /* the output is this array */
solution[1]=a1; /* which contains in each */
solution[2]=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]);
}