Link to home
Start Free TrialLog in
Avatar of GJMill79
GJMill79

asked on

Recursive Function to calculate pi (Using gregory series)

I need to know how to write a recursive function to calculate pi using the gregory series...based on user input.  

For example I'm requesting the user to input the number of terms to calculate pi based on the Gregory series.  Please help!

Thank you  


Avatar of mokule
mokule
Flag of Poland image

Is it homework?
You can find some theory there
http://mathworld.wolfram.com/GregorySeries.html
Avatar of GJMill79
GJMill79

ASKER

Yes it is...thank you for the comment.

I know how to calculate pi with the gregory series in C without a function but I can't figure out how to utilize a recursive function to calculate pi using the gregory series.
Although this doesn't seem to work (I recieve a seg fault)...I thought I would post my guess

double piby4(int terms)
{

        if (terms==1)
        return(piby4(1.0/terms+terms-1));
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1));
       
        else
           return (0);
       
}

down in int main

double pi, pidiv4;
int terms;

printf("How many terms do you want to use to calculate pi?  ");
fgets(buffer, sizeof(buffer), stdin);      
sscanf(buffer, "%d", &terms);
                  
                          pidiv4=piby4(terms);
                          pi=pidiv4*=4;
              
printf("The term you entered is %d\n", terms);
printf("The pi value is %0.6f\n", pi);
The error is You never stops calling piby4
Oops not only
What do You mean here
piby4(1.0/terms+terms-1);
In declaration piby4 has integer argument and in call You try to pass float
I'm thinking that since the Gregory series is pi/4=1/1-1/3+1/5-1/7...
Bases on the user input for # of terms That I'm returning to int main() piby4(1.0/terms+terms-1) ...until C decrements terms to 1....but I'm not exactly sure how a recursive function works.... I need help in putting it together basically.
If I'm trying to pass an int how do I return a float?
1.
First tell me what do You mean by terms
1,2,3,4, etc
or
1,3,5, etc

2.
If Your intension was to stop here You shouldn't call piby4
        if (terms==1)
        return(piby4(1.0/terms+terms-1));

3.What argument You want to pass to piby4
Passing argument has nothing to do with returning value.

for example
double fun(int k)
{
if(k==1)
  return 13.25;
else
  return 54.321;
}
You passing integer and returning double.
By terms I mean 1/1 equals 1 term, 1/1-1/3 equals two terms, 1/1 - 1/3 + 1/5 equals three terms etc...

"In declaration piby4 has integer argument and in call You try to pass float"...not sure what you mean by this

double piby4(int terms)
{
        if (terms==1)
        return(piby4(1.0/terms+terms-1));
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1));
       
        else
           return (0);
}
--------------------------------
Your argument in this call
piby4(1.0/terms+terms-1)
is
1.0/terms+terms-1
and it float
Do I need to change double piby4 to (double terms) instead of (int terms)

Is the basic setup of my function correct?  
double piby4(int terms)     // it is correct
{
        if (terms==1)
        return(piby4(1.0/terms+terms-1));       // don'call piby4 here - only return 1.
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));   // should be piby4(terms-1) - something
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) + something
       
        else                         // it is not necesarry
           return (0);             // it is not necesarry

}

If You guess what is something it is done and You understand.
is it -1 and +1 ?
so your're saying return(piby4(terms-1) - something)?---is it -1.0/terms
I'm sorry, no
Look at the basic formula
for example
piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9    right?
and
piby4(4) should be equivalent to 1/1-1/3+1/5-1/7    right?
so
piby(5) = piby(4) + what

Quite near but we stated that term is 1,2,3,4 etc
Right!

piby4(5)=piby4(4) + (1/term+2)?
No. Back to my last example for term 5 what shoul it be?
Please look at the formula at my first link. It should be clear then
piby4(5)=piby4(4) + piby4(terms+1)
Wrong direction
piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9    right?
and
piby4(4) should be equivalent to 1/1-1/3+1/5-1/7    right?
so
piby4(5) =  piby4(4) +1/9      right?
I understand that but why would I put  +1/9 in my function if the terms change based on user input?
So your saying:
else if (terms %2)
       return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) +something
     
       so return(piby4(terms-1)+1/9?  How does the function know what to divide?


           
No You should put some expression that evaluates to -1/7 for term 4, 1/9 for term 5,  -1/11 for term 6, 1/13 for term 7,
how will that work if the user can input any amount of terms?
Do You look at the link I provided at the beginning.
Please write me the denominator of the first formula on the page
2k-1...are you saying that I need to use this formula in my function?
I don't now what You mean by this formula.
You use this formula by recursion and this expression about we are talking so long is
1./(2*term-1).
Please we must quickly come to an end.
It's getting here very very late.
I'm sorry but I'm so confused now...

I'm not sure what I'm suppose to return?

do I put the formula in int main or the function?
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for all your help...since time is running out for you, I'll try this...
the line
        else if (terms %2)
better change with
        else
Greetings from Poland
We can come to this tomorrow. If You don't understand something yet.
Thank you Mokule that answers my question perfectly!  I'll look you up in the future if I have anymore programming questions.

Greetings from the U.S.!