I have a problem to replace "pow" in my program below (infinite geometric series).If the input a=2 and r=.99999 the program will get the answer so slow. Otherwise the program output is correct.
Could anyone help me to replace the pow with something else.
int main()
{
int n, i=1, x=0, b=i-1;
double a, r, number, suma, sumb=0;
cout << "Please enter a starting number for your infinite geometric series\n";
cin >> a;
cout << "Enter the number between -1 and 1 for the series to increment by\n";
cin >> r;
while (r>1 && r<-1)
{
cout << "The increment must be between -1 and 1";
cout << endl;
cout << "Enter the number between -1 and 1 for the series to increment by\n";
cin >> r;
}
suma = a /(1-r); //compute the sum by the formula
//check the condition for both of sum
while (fabs (suma-sumb)> fabs (suma*0.0000001))
{
number = a*(pow(r,b)); //THIS THE PART THAT I HAVE TO REPLACE
sumb = sumb + number; //compute the sum by add the number
i++;
}
cout << endl;
cout.precision (15);
cout << scientific << "Sum by formula S= a/(1-r) is " << suma;
cout << endl;
cout << scientific << "Sum by add the series numbers is " << sumb;
cout << endl;
the number = a*pow(r,b) does not change, so you must calculate it once before the cycle, and only use it inside the cycle.
But i'm not sure of the correctness of the logics...
the problem is in the "pow" I have to change with something else. Example for the area of circle (phi*pow(radius,2)), I have to change to (phi*radius*radius).
The result with pow is too slow for some numbers input.
Avels,
You can quicken your program manyfold by rewriting the main cycle as
1 2 3 4 5 6
number = a*(pow(r,b));
while (fabs (suma-sumb)> fabs (suma*0.0000001))
{
sumb = sumb + number;
i++;
}
because the pruduction of a*pow(r,b) does not change from iteration to iteration.
But i guess the algorithm is not correct, and to optimize a senseless program is senseless.
sorry.....the program at the top have a little bit mistake, I pulled the declare the b at the top. Program at the bottom suppose work correctly.
I should make a program that read 2 input a (the initial of the geometric series) and r (the increment, the value is -1<r<1). Then calculate the sum of the geometric series in 2 different ways, first by formula sum=a/(1-r) and the other is by adding every individual terms until the answer match for 7 digits. For the last sum, two real values have match with this condition |suma-sumb|<|suma*10^-n|. The program below is correct. For an example you can put a = 1 and r =0.1. Then the sum is 1.1111111 (the result for both ways is the same pattern for 7 digits). You can try for different number also. But for some number. example if the a=2 and the r=0.9999, it will take a long time for the computer give the result. The problem is in the pow. I should not use "pow" but something else.
@Melkiy, you still use pow. I have to avoid pow in my program.
int main()
{
int n, i=1, x=0;
double a, r, number, suma, sumb=0;
cout << "Please enter a starting number for your infinite geometric series\n";
cin >> a;
cout << "Enter the number between -1 and 1 for the series to increment by\n";
cin >> r;
while (r>1 && r<-1)
{
cout << "The increment must be between -1 and 1";
cout << endl;
cout << "Enter the number between -1 and 1 for the series to increment by\n";
cin >> r;
}
suma = a /(1-r); //compute the sum by the formula
//check the condition for both of sum
while (fabs (suma-sumb)> fabs (a*0.0000001))
{
int b=i-1;
number = a*(pow(r,b));
sumb = sumb + number; //compute the sum by add the number
i++;
}
cout << endl;
cout.precision (15);
cout << scientific << "Sum by formula S= a/(1-r) is " << suma;
cout << endl;
cout << scientific << "Sum by add the series numbers is " << sumb;
cout << endl;