How to enter this program?

Hello, I have tried unsuccessfully for a couple hours to enter a program that loops until a number that is even, divisible by both 13 and 16, and whose square root is greater than 120 is found. I have reached a wall, so I have come to ask for help. Would using a while statement be the first step? Please don't solve this problem, I only ask for help getting it going. Yes, this is basic stuff, but I still have little understanding on it.

 Respuesta aceptada

Roger Stafford
Roger Stafford el 25 de Oct. de 2014

1 voto

You need to translate each of those conditions into a matlab logical proposition. For example if n is an integer you are investigating, try dividing it by 13, then comparing the 'round' of the quotient with the original quotient to see if it is still an integer after the division. And yes, the 'while' function is the right thing to use here. Keep it looping until all the conditions turn out to be true for the same integer, for which you can make great use of the "&" operation.

5 comentarios

Rick Rosson
Rick Rosson el 25 de Oct. de 2014
Editada: Rick Rosson el 25 de Oct. de 2014
You may also find that the mod function is helpful in this particular case. This function returns the modulus of two integers, which is the remainder that is left after dividing the first integer by the second integer. So, for example:
mod(45,13)
returns 6, whereas
mod(52,13)
returns 0.
This provides a very easy way to test for divisibility. For example:
if mod(n,13)
disp('n is not divisible by 13');
else
disp('n is divisible by 13');
end
Roger Stafford
Roger Stafford el 25 de Oct. de 2014
Yes, that's a little easier than writing
round(n/13) == n/13
Colton
Colton el 25 de Oct. de 2014
Editada: Colton el 25 de Oct. de 2014
Okay, so I said
a = 2;
while sqrt(a) <= 120 && mod(a,13) && mod(a,16)
a = a + 2;
end
But this returns a = 16, satisfying only the final condition. Could you point me in the right direction? Thank you so much for your help so far though.
Rick Rosson
Rick Rosson el 25 de Oct. de 2014
Try using || instead of &&.
Colton
Colton el 26 de Oct. de 2014
@Rick, thank you. That fixed my issues and I was able to find the correct value! And thanks everyone else for the help!

Iniciar sesión para comentar.

Más respuestas (2)

Rick Rosson
Rick Rosson el 25 de Oct. de 2014
Editada: Rick Rosson el 25 de Oct. de 2014

1 voto

Here is a faster and simpler approach:
a = 0;
while a < 120^2
a = a + 13*16;
end

1 comentario

per isakson
per isakson el 26 de Oct. de 2014
Note &nbsp "square root is greater than 120"

Iniciar sesión para comentar.

per isakson
per isakson el 26 de Oct. de 2014
Editada: per isakson el 26 de Oct. de 2014

0 votos

Try
>> factor( cssm )
ans =
2 2 2 2 2 5 7 13
where cssm is
function ix = cssm()
% chose a start value, which is larger than 120^2 and divisible by 16
ix = 120*120+16;
while ne( mod(ix,13), 0 )
ix = ix + 16;
end
end

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Oct. de 2014

Comentada:

el 26 de Oct. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by