Finding quotient using while loop

In summary, a while loop is a control structure in programming that allows for the repetition of instructions while a certain condition is true, leading to more efficient and concise code. To find a quotient using a while loop, the loop would continuously subtract the divisor from the dividend until the remainder is less than the divisor. This method can also be used for decimal numbers, but would require additional code for handling decimal places and rounding. However, there are limitations to using a while loop, such as the possibility of an infinite loop if the condition is never met or if there is a logical error. A while loop is appropriate to use when the number of iterations needed is unknown and depends on a condition, and when the task can be broken down into smaller repetitive
  • #1
DryRun
Gold Member
838
4
Suppose that the number ##\pi## is divided by 2. The resulting quotient is divided by 2 again. This process is continued till the current quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?

Here is my attempt:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  
 
quo  =  pi / 2;  
num(r) = quo;
r = r+1;
end 

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

The processing takes about 20 or 30 seconds and then i get this error in the command window:

Out of memory. Type HELP MEMORY for your options.

Error in piquo (line 7)
num(r) = quo;
 
Physics news on Phys.org
  • #2
sharks said:
Suppose that the number ##\pi## is divided by 2. The resulting quotient is divided by 2 again. This process is continued till the current quotient is less than or equal to 0.01. What is the largest quotient that is greater than 0.01?

Here is my attempt:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  
 
quo  =  pi / 2;  
num(r) = quo;
r = r+1;
end 

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

The processing takes about 20 or 30 seconds and then i get this error in the command window:

Out of memory. Type HELP MEMORY for your options.

Error in piquo (line 7)
num(r) = quo;

It might be better to take the quo=pi/2 statement outside of the while loop ...
 
  • #3
Here is the modified script:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  

num(r) = quo;
r = r+1;
end 

quo  =  pi / 2;  

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

But Matlab just keeps processing, and then crashes.

Maybe you meant to use a nested while loop? I don't know how that would work.

I'm quite sure that i need to use a while loop in this problem, as i don't know how many iterations are required for the quotient to become less than 0.01. num(r) is an array that represents the index which contains the corresponding quotient, based on the number of times r that the while loop is executed.
 
  • #4
sharks said:
Here is the modified script:

Code:
r = 1;
quo = 1;

while  (quo  >  0.01)  

num(r) = quo;
r = r+1;
end 

quo  =  pi / 2;  

disp('The largest quotient greater than 0.01 is: ' ,(num(r-1)) )

But Matlab just keeps processing, and then crashes.

Maybe you meant to use a nested while loop? I don't know how that would work.

I'm quite sure that i need to use a while loop in this problem, as i don't know how many iterations are required for the quotient to become less than 0.01. num(r) is an array that represents the index which contains the corresponding quotient, based on the number of times r that the while loop is executed.

No, what I meant was your loop kept setting quo to pi/2 as its first action, hence why it didn't terminate. You have a similar problem with your revised loop in that you never change quo.

Have a look at the attached Mathcad example of what I think you want to achieve.
 

Attachments

  • phys - 12 06 15 iteration 01.jpg
    phys - 12 06 15 iteration 01.jpg
    6.3 KB · Views: 557
  • #5
Here is the revised code:

Code:
r = 1;
quo = pi;

while  (quo  >  0.01)  

quo  =  quo / 2;  
num(r) = quo;
r = r+1;
end 

disp(num(r-2))

I believe it's correct, as i get the answer: 0.0123

However, i cannot display the line: The largest quotient greater than 0.01 is: 0.0123

I tried this format but it didn't work: disp('The largest quotient greater than 0.01 is: ' ,num(r-2) )
 
  • #6
sharks said:
Here is the revised code:

Code:
r = 1;
quo = pi;

while  (quo  >  0.01)  

quo  =  quo / 2;  
num(r) = quo;
r = r+1;
end 

disp(num(r-2))

I believe it's correct, as i get the answer: 0.0123

However, i cannot display the line: The largest quotient greater than 0.01 is: 0.0123

I tried this format but it didn't work: disp('The largest quotient greater than 0.01 is: ' ,num(r-2) )

I'm not a Matlab user, so don't take what I say as Gospel, but I don't think disp works like that. According to the on-line Help, you've got convert the number to a string and, for example, concatenate it with your string. See http://www.mathworks.co.uk/help/techdoc/ref/disp.html for details.

PS. The code looks better now and the result seems OK.
 
  • #7
OK, thanks for your help, NemoReally.
 
  • #8
sharks said:
OK, thanks for your help, NemoReally.
No worries.
 

1. What is a while loop?

A while loop is a control structure in programming that allows a set of instructions to be repeated while a certain condition is true. This allows for more efficient and concise code compared to using multiple if statements.

2. How do you use a while loop to find a quotient?

To find a quotient using a while loop, you would need to set up a loop that continuously subtracts the divisor from the dividend until the remainder is less than the divisor. The number of times the loop runs would be the quotient.

3. Can a while loop be used to find the quotient of decimal numbers?

Yes, a while loop can be used to find the quotient of decimal numbers. However, it would require additional code to handle decimal places and rounding.

4. Are there any limitations to using a while loop to find a quotient?

One limitation of using a while loop to find a quotient is that it can potentially result in an infinite loop if the condition is never met or if there is a logical error in the code. It is important to ensure that the loop will eventually terminate.

5. When is it appropriate to use a while loop to find a quotient?

A while loop is appropriate to use when the number of iterations needed to find a quotient is not known beforehand and depends on a certain condition. It is also useful when the task can be broken down into smaller, repetitive steps.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
4
Views
889
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Special and General Relativity
Replies
11
Views
182
  • Beyond the Standard Models
Replies
9
Views
483
  • New Member Introductions
Replies
1
Views
68
Replies
5
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
865
Back
Top