943,589 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2253
  • C++ RSS
Oct 27th, 2005
0

homework assignment help

Expand Post »
hi. new here. Im a freshman at UCSB and im taking my first c++ class. I got this assignment in class and i was hoping for some tips.

The value of pi to 15 decimal places is PI = 3.141592653589793 . . . Your job is to write a
program that can be used to find decent approximations to PI. More specifically, your program must find
integers N and D such that the fraction N/D is the best possible approximation to PI and such that D <
1,000,000.

I have already set up nested while loops to chug through the fractions and limited the numerator to n <= 3145926. I also set a if condition right in between the while to reduce the numer of times it goest through the calculations by narrowing the numbers between 3 and 4( like pi). such that if( 3*d < n && 4*d > n){

Now the thing works but it takes about 6 hrs to run and for this project the instructor wants us to make one that runs in 2 min or less. I was wondering if anyone has any tips that may help.
thank you
bw
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigwillis234 is offline Offline
2 posts
since Oct 2005
Oct 27th, 2005
0

Re: homework assignment help

If you post code (using code tags) someone may be willing to take a stab at helping you. Without posting code it's like asking "how can I reduce the time it takes me to file out my tax return?".
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 27th, 2005
0

Re: homework assignment help

Well, try all numbers from D to 1,000,000. I was able to write a program that calculates this in under 20 seconds.

BestPi: 3.141592653588651 3126535/995207

I used one loop that went from D equals 1 to 1,000,000, the interior loop is where it gets tricky. Like you said, the ratio of N/D has to be between 3 and 4...

3 < N/D < 4
3*D < N < 4*D

That's all you get without posting your code.

-Fredric
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Oct 27th, 2005
0

Re: homework assignment help

Quote originally posted by bigwillis234 ...
Now the thing works but it takes about 6 hrs to run
LMAO :lol: :lol: :lol: Sorry for laughing so much! But the last time I heard something like that was way back in 1987 on an old Unix computer -- the program (no, I didn't write it) took 48 hours to run. I rewrite it in C and got it to run in under 10 minutes.

Post your program and I'm certain someone will help you with it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Oct 27th, 2005
0

Re: homework assignment help

this is what i got now. it still takes a long time. like an hour



int n, d=1, n2, d2;
double pie= 3.141592643563118, guess, piguess = 3.15;

int main()
{

while(d <= 1000000) { /* d goes to 1000000 */
n=3.1*d; /* started n at 3.1 * to get right to pi area*/
while(n <= 3.2*d ){ /*shortened loop for values */


guess = fabs(((double)n/d) - pie); /*abs value to compare guess*/
if( guess < piguess ){
n2= n;
d2= d;
piguess= guess;

}n++;
}d++;
}

printf( "%d / %d", n2, d2);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigwillis234 is offline Offline
2 posts
since Oct 2005
Oct 27th, 2005
0

Re: homework assignment help

Algorithm efficiency is dependant upon eliminating fluff. For example, eliminate reducable fractions. (You've already tested it before... 1/3 ======= 2/6)
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Oct 27th, 2005
0

Re: homework assignment help

And you may want to break out of the loop when you find an answer.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 28th, 2005
0

Re: homework assignment help

A method based on continued fractions will get you the answer in less than a millisecond.

(Ooh I'm being so unhelpful )
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++/OpenGL/GLUI Linker problems
Next Thread in C++ Forum Timeline: sorting algorithm not working correctly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC