Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using loops, need number to be an odd integer

Subject: using loops, need number to be an odd integer

From: Marina

Date: 18 Mar, 2012 22:42:10

Message: 1 of 9

Hi,
I am currently working on a homework assignment and on my last problem but I am having some trouble.

The question is:
Write a program in a script file that finds the smallest odd integer that is divisible by 11 and whose square root is greater than 132. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message “The required number is:” and then prints the number.

So far this is what I got in my script file:
n = 1;
while (sqrt(n)<132)||(rem(n,11)~=0)
n = n+1;
if rem(n,2)==1;
n = n+1;
end
end

str = ['The number is: ' num2str(n)];
disp(str);

The answer to this is 17424
therefore I know something in the code is wrong because that is an even number not an odd number. The answer should be 17435 but I am unsure how to edit my code to get the right answer. I would appreciate any help.

Thank You

Subject: using loops, need number to be an odd integer

From: Roger Stafford

Date: 18 Mar, 2012 23:18:10

Message: 2 of 9

"Marina" wrote in message <jk5oc2$e0m$1@newscl01ah.mathworks.com>...
> Hi,
> I am currently working on a homework assignment and on my last problem but I am having some trouble.
>
> The question is:
> Write a program in a script file that finds the smallest odd integer that is divisible by 11 and whose square root is greater than 132. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message “The required number is:” and then prints the number.
>
> So far this is what I got in my script file:
> n = 1;
> while (sqrt(n)<132)||(rem(n,11)~=0)
> n = n+1;
> if rem(n,2)==1;
> n = n+1;
> end
> end
>
> str = ['The number is: ' num2str(n)];
> disp(str);
>
> The answer to this is 17424
> therefore I know something in the code is wrong because that is an even number not an odd number. The answer should be 17435 but I am unsure how to edit my code to get the right answer. I would appreciate any help.
>
> Thank You
- - - - - - - - -
  All you need to have inside the while-loop is n = n+2. Starting with n = 1, all n's will then be odd.

Roger Stafford

Subject: using loops, need number to be an odd integer

From: Matt J

Date: 19 Mar, 2012 06:34:11

Message: 3 of 9

Another possibility


n = 11;
while sqrt(n)<132
 n = n+22;
end

Subject: using loops, need number to be an odd integer

From: Matt J

Date: 19 Mar, 2012 06:41:10

Message: 4 of 9

"Matt J" wrote in message <jk6k13$sto$1@newscl01ah.mathworks.com>...
> Another possibility
>
>
> n = 11;
> while sqrt(n)<132
> n = n+22;
> end
=============

I missed the requirement that the loop start at n=1. Here's a modification to include it,

n = 1;
while sqrt(n)<132
 if n==1,
     n=11;
 else
     n=n+22;
 end
end

Subject: using loops, need number to be an odd integer

From: Greg Heath

Date: 19 Mar, 2012 19:25:21

Message: 5 of 9

On Mar 19, 2:41 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> "Matt J" wrote in message <jk6k13$st...@newscl01ah.mathworks.com>...
> > Another possibility
>
> > n = 11;
> > while sqrt(n)<132
> >  n = n+22;
> > end
>
> =============
>
> I missed the requirement that the loop start at n=1. Here's a modification to include it,
>
> n = 1;
> while sqrt(n)<132
>  if n==1,
>      n=11;
>  else
>      n=n+22;
>  end
> end

Subject: using loops, need number to be an odd integer

From: Greg Heath

Date: 19 Mar, 2012 19:27:24

Message: 6 of 9

On Mar 19, 2:41 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> "Matt J" wrote in message <jk6k13$st...@newscl01ah.mathworks.com>...
> > Another possibility
>
> > n = 11;
> > while sqrt(n)<132
> >  n = n+22;
> > end
>
> =============
>
> I missed the requirement that the loop start at n=1. Here's a modification to include it,
>
> n = 1;
> while sqrt(n)<132
>  if n==1,
>      n=11;
>  else
>      n=n+22;
>  end
> end

Subject: using loops, need number to be an odd integer

From: Greg Heath

Date: 19 Mar, 2012 19:29:40

Message: 7 of 9

On Mar 19, 2:41 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> "Matt J" wrote in message <jk6k13$st...@newscl01ah.mathworks.com>...
> > Another possibility
>
> > n = 11;
> > while sqrt(n)<132
> > n = n+22;
> > end
>
> =============
>
> I missed the requirement that the loop start at n=1. Here's a modification to include it,
>
> n = 1;
> while sqrt(n)<132
> if n==1,
> n=11;
> else
> n=n+22;
> end
> end

Or,if you want a faster loop

tic
 n = 1;
 while sqrt(n)<132
       if n==1,
            n = 11*(1+2*round(132^2/2/11))
      else
            n = n+22;
      end
 end
toc

Hope this helps

Greg

Subject: using loops, need number to be an odd integer

From: Chin

Date: 11 Dec, 2013 14:53:05

Message: 8 of 9

"Marina" wrote in message <jk5oc2$e0m$1@newscl01ah.mathworks.com>...
> Hi,
> I am currently working on a homework assignment and on my last problem but I am having some trouble.
>
> The question is:
> Write a program in a script file that finds the smallest odd integer that is divisible by 11 and whose square root is greater than 132. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message “The required number is:” and then prints the number.
>
> So far this is what I got in my script file:
> n = 1;
> while (sqrt(n)<132)||(rem(n,11)~=0)
> n = n+1;
> if rem(n,2)==1;
> n = n+1;
> end
> end
>
> str = ['The number is: ' num2str(n)];
> disp(str);
>
> The answer to this is 17424
> therefore I know something in the code is wrong because that is an even number not an odd number. The answer should be 17435 but I am unsure how to edit my code to get the right answer. I would appreciate any help.
>
> Thank You


-----------------------------------------
In the question is using AND how come not using & ?

Subject: using loops, need number to be an odd integer

From: Steven Lord

Date: 11 Dec, 2013 15:14:02

Message: 9 of 9


"Chin " <yuanyee7@gmail.com> wrote in message
news:l89u8h$394$1@newscl01ah.mathworks.com...
> "Marina" wrote in message <jk5oc2$e0m$1@newscl01ah.mathworks.com>...
>> Hi,
>> I am currently working on a homework assignment and on my last problem
>> but I am having some trouble.
>>
>> The question is:
>> Write a program in a script file that finds the smallest odd integer that
>> is divisible by 11 and whose square root is greater than 132. Use a loop
>> in the program. The loop should start from 1 and stop when the number is
>> found. The program prints the message The required number is: and then
>> prints the number.
>>
>> So far this is what I got in my script file:
>> n = 1;
>> while (sqrt(n)<132)||(rem(n,11)~=0)
>> n = n+1;
>> if rem(n,2)==1;
>> n = n+1;
>> end
>> end
>>
>> str = ['The number is: ' num2str(n)];
>> disp(str);
>>
>> The answer to this is 17424
>> therefore I know something in the code is wrong because that is an even
>> number not an odd number. The answer should be 17435 but I am unsure how
>> to edit my code to get the right answer. I would appreciate any help.
>>
>> Thank You
>
>
> -----------------------------------------
> In the question is using AND how come not using & ?

In addition, rather than incrementing by 1 and then checking if you need to
increment by another 1: you start off with an odd number. How would you get
to the next odd number in one step rather than two steps?

You can also do this without a loop or even calling SQRT. There's a number
you can easily compute that does not solve the problem but is very close to
the solution.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us