homework assignment help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 2
Reputation: bigwillis234 is an unknown quantity at this point 
Solved Threads: 0
bigwillis234's Avatar
bigwillis234 bigwillis234 is offline Offline
Newbie Poster

homework assignment help

 
0
  #1
Oct 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: homework assignment help

 
0
  #2
Oct 27th, 2005
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?".
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: homework assignment help

 
0
  #3
Oct 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: homework assignment help

 
0
  #4
Oct 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 2
Reputation: bigwillis234 is an unknown quantity at this point 
Solved Threads: 0
bigwillis234's Avatar
bigwillis234 bigwillis234 is offline Offline
Newbie Poster

Re: homework assignment help

 
0
  #5
Oct 27th, 2005
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);
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: homework assignment help

 
0
  #6
Oct 27th, 2005
Algorithm efficiency is dependant upon eliminating fluff. For example, eliminate reducable fractions. (You've already tested it before... 1/3 ======= 2/6)
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: homework assignment help

 
0
  #7
Oct 27th, 2005
And you may want to break out of the loop when you find an answer.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: homework assignment help

 
0
  #8
Oct 28th, 2005
A method based on continued fractions will get you the answer in less than a millisecond.

(Ooh I'm being so unhelpful )
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC