We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Recursive Function to calculate pi (Using gregory series)

GJMill79
GJMill79 asked
on
Medium Priority
2,236 Views
Last Modified: 2010-04-15
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  


Comment
Watch Question

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

Author

Commented:
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.

Author

Commented:
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);

Commented:
The error is You never stops calling piby4

Commented:
Oops not only
What do You mean here
piby4(1.0/terms+terms-1);

Commented:
In declaration piby4 has integer argument and in call You try to pass float

Author

Commented:
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.

Author

Commented:
If I'm trying to pass an int how do I return a float?

Commented:
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

Commented:
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.

Author

Commented:
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...

Author

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

Commented:
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

Author

Commented:
Do I need to change double piby4 to (double terms) instead of (int terms)

Is the basic setup of my function correct?  

Commented:
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.

Author

Commented:
is it -1 and +1 ?

Author

Commented:
so your're saying return(piby4(terms-1) - something)?---is it -1.0/terms

Commented:
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

Commented:
Quite near but we stated that term is 1,2,3,4 etc

Author

Commented:
Right!

piby4(5)=piby4(4) + (1/term+2)?

Commented:
No. Back to my last example for term 5 what shoul it be?

Commented:
Please look at the formula at my first link. It should be clear then

Author

Commented:
piby4(5)=piby4(4) + piby4(terms+1)

Commented:
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?

Author

Commented:
I understand that but why would I put  +1/9 in my function if the terms change based on user input?

Author

Commented:
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?


           

Commented:
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,

Author

Commented:
how will that work if the user can input any amount of terms?

Commented:
Do You look at the link I provided at the beginning.
Please write me the denominator of the first formula on the page

Author

Commented:
2k-1...are you saying that I need to use this formula in my function?

Commented:
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.

Author

Commented:
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?
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thank you very much for all your help...since time is running out for you, I'll try this...

Commented:
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.

Author

Commented:
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.!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.