Solved

Recursive Function to calculate pi (Using gregory series)

Posted on 2004-03-28
36
1,590 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  


0
Comment
Question by:GJMill79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 18
36 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10700269
Is it homework?
You can find some theory there
http://mathworld.wolfram.com/GregorySeries.html
0
 

Author Comment

by:GJMill79
ID: 10700294
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
 

Author Comment

by:GJMill79
ID: 10700316
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!

 
LVL 17

Expert Comment

by:mokule
ID: 10700342
The error is You never stops calling piby4
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700361
Oops not only
What do You mean here
piby4(1.0/terms+terms-1);
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700369
In declaration piby4 has integer argument and in call You try to pass float
0
 

Author Comment

by:GJMill79
ID: 10700379
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
 

Author Comment

by:GJMill79
ID: 10700396
If I'm trying to pass an int how do I return a float?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700402
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
 
LVL 17

Expert Comment

by:mokule
ID: 10700411
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
 

Author Comment

by:GJMill79
ID: 10700415
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
 

Author Comment

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

0
 
LVL 17

Expert Comment

by:mokule
ID: 10700451
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
 

Author Comment

by:GJMill79
ID: 10700471
Do I need to change double piby4 to (double terms) instead of (int terms)

Is the basic setup of my function correct?  
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700493
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
 

Author Comment

by:GJMill79
ID: 10700525
is it -1 and +1 ?
0
 

Author Comment

by:GJMill79
ID: 10700533
so your're saying return(piby4(terms-1) - something)?---is it -1.0/terms
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700551
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
 
LVL 17

Expert Comment

by:mokule
ID: 10700559
Quite near but we stated that term is 1,2,3,4 etc
0
 

Author Comment

by:GJMill79
ID: 10700588
Right!

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

Expert Comment

by:mokule
ID: 10700601
No. Back to my last example for term 5 what shoul it be?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700620
Please look at the formula at my first link. It should be clear then
0
 

Author Comment

by:GJMill79
ID: 10700622
piby4(5)=piby4(4) + piby4(terms+1)
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700634
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
 

Author Comment

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

Author Comment

by:GJMill79
ID: 10700682
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
 
LVL 17

Expert Comment

by:mokule
ID: 10700693
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
 

Author Comment

by:GJMill79
ID: 10700707
how will that work if the user can input any amount of terms?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700731
Do You look at the link I provided at the beginning.
Please write me the denominator of the first formula on the page
0
 

Author Comment

by:GJMill79
ID: 10700750
2k-1...are you saying that I need to use this formula in my function?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700761
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
 

Author Comment

by:GJMill79
ID: 10700782
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
 
LVL 17

Accepted Solution

by:
mokule earned 500 total points
ID: 10700801
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
 

Author Comment

by:GJMill79
ID: 10700813
Thank you very much for all your help...since time is running out for you, I'll try this...
0
 
LVL 17

Expert Comment

by:mokule
ID: 10700834
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
 

Author Comment

by:GJMill79
ID: 10701060
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
negation in C function 14 179
Problem to scan all sheets 3 134
change colour of repeater control in asp.net c# 7 162
Memory going from 12gb to 64gb or 96gb. worth it? 15 222
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question