Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1707
  • Last Modified:

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  


0
GJMill79
Asked:
GJMill79
  • 18
  • 18
1 Solution
 
mokuleCommented:
Is it homework?
You can find some theory there
http://mathworld.wolfram.com/GregorySeries.html
0
 
GJMill79Author 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.
0
 
GJMill79Author 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);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mokuleCommented:
The error is You never stops calling piby4
0
 
mokuleCommented:
Oops not only
What do You mean here
piby4(1.0/terms+terms-1);
0
 
mokuleCommented:
In declaration piby4 has integer argument and in call You try to pass float
0
 
GJMill79Author 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.
0
 
GJMill79Author Commented:
If I'm trying to pass an int how do I return a float?
0
 
mokuleCommented:
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
0
 
mokuleCommented:
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.
0
 
GJMill79Author 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...

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

0
 
mokuleCommented:
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
0
 
GJMill79Author Commented:
Do I need to change double piby4 to (double terms) instead of (int terms)

Is the basic setup of my function correct?  
0
 
mokuleCommented:
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.
0
 
GJMill79Author Commented:
is it -1 and +1 ?
0
 
GJMill79Author Commented:
so your're saying return(piby4(terms-1) - something)?---is it -1.0/terms
0
 
mokuleCommented:
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

0
 
mokuleCommented:
Quite near but we stated that term is 1,2,3,4 etc
0
 
GJMill79Author Commented:
Right!

piby4(5)=piby4(4) + (1/term+2)?
0
 
mokuleCommented:
No. Back to my last example for term 5 what shoul it be?
0
 
mokuleCommented:
Please look at the formula at my first link. It should be clear then
0
 
GJMill79Author Commented:
piby4(5)=piby4(4) + piby4(terms+1)
0
 
mokuleCommented:
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?
0
 
GJMill79Author Commented:
I understand that but why would I put  +1/9 in my function if the terms change based on user input?
0
 
GJMill79Author 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?


           
0
 
mokuleCommented:
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,
0
 
GJMill79Author Commented:
how will that work if the user can input any amount of terms?
0
 
mokuleCommented:
Do You look at the link I provided at the beginning.
Please write me the denominator of the first formula on the page
0
 
GJMill79Author Commented:
2k-1...are you saying that I need to use this formula in my function?
0
 
mokuleCommented:
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.
0
 
GJMill79Author 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?
0
 
mokuleCommented:
double piby4(int terms)     // it is correct
{
        if (terms==1)
//        return(piby4(1.0/terms+terms-1));       // don'call piby4 here - only return 1.
          return 1.;
       
        else if (terms %2==0)
//           return(piby4(-1.0/terms+terms-1));   // should be piby4(terms-1) - something
             return(  piby4(terms-1) + 1./(2*terms-1)   );
        else if (terms %2)
//           return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) + something
             return(  piby4(terms-1) - 1./(2*terms-1)   );
       
//        else                         // it is not necesarry
//           return (0);             // it is not necesarry

}
0
 
GJMill79Author Commented:
Thank you very much for all your help...since time is running out for you, I'll try this...
0
 
mokuleCommented:
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.
0
 
GJMill79Author 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.!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 18
  • 18
Tackle projects and never again get stuck behind a technical roadblock.
Join Now