• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

I'm too stupid to program

The following program gives a rational approximation for an irrational number (pi,e,sqrt(2), etc.). How do I tell maple only to print out the current p/q if it is closer to r than all the previous p/q's? Currently I'm just printing out thousands of numbers. 😱

ratapprox:=proc(r,N)
local rr,p,q,z;

rr:=evalf(r);
p := 1; q := 1;

while N > q do
z:=(p/q);

if evalf(z)<rr then
p:=p+1;
print(z);

else
q:=q+1;
print(z);

end if;

end do;

end proc;
 
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end
 
Originally posted by: habib89
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end

basically I need to add something that says that if the current error=abs(p/q-r) is less than ALL the previous errors, print out the current p/q

I only know how to tell Maple to print out the current p/q if it's error is smaller than the previous p/q's error
 
Use an if statement to perform a test prior to outputing.
After outputing, store the variable that triggered the condition and test against it.

Also try the S/W forums
 
Originally posted by: EagleKeeper
Use an if statement to perform a test prior to outputing.
After outputing, store the variable that triggered the condition and test against it.

Also try the S/W forums

Thanks for the help. It's now only priting about 10 numbers. Unfortunately now I think the numbers are just slighly off.
 
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


😛

In the "right forum" I probably wouldn't get any responses.

that's because they spend all their time looking for the ; they are missing 😉
 
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


😛

In the "right forum" I probably wouldn't get any responses.

The problem is that people use that excuse and that is partly the reason that those forums aren't visited as much because people post these threads here and not there.
 
New code:

ratapprox:=proc(r,N)
local rr,p,q,err,z;

rr:=evalf(r);
p := 1; q := 1;
err:=evalf(r+1);

while N > q do
z:=(p/q);

if evalf(z)<rr then
p:=p+1;

if evalf(abs(p/q-r))<err then
print(p/q);
err:=evalf(abs(p/q-r));

end if;

else
q:=q+1;
if evalf(abs(p/q-r))<err then
print(p/q);
err:=evalf(abs(p/q-r));

end if;

end if;

end do;

end proc;
 
Originally posted by: habib89
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end

Programming tools these days take care of syntax problems for you.
 
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


😛

In the "right forum" I probably wouldn't get any responses.

A response will not come as quickly, but they will usually come.

/HINT and if the games were removed, then better quality would also exist

And a lot less flaming and sarcasm will exist for posting in the wrong forum.

There are quality S/W people that do frequent the S/W forum.

 
Back
Top